Removing Children of DIV after certain Number

后端 未结 5 475
庸人自扰
庸人自扰 2020-12-30 09:59

I have this HTML

5条回答
  •  梦谈多话
    2020-12-30 10:12

    You're looking for :nth-child: http://api.jquery.com/nth-child-selector/

    It works like this:

    $('.control div:nth-child(2), .control div:nth-child(3), .control div:nth-child(4)').remove();
    

    Note that :nth-child uses one-based indexing, so the first element has index 1.

    UPDATE: In response to this question the OP posted in a comment

    If i dont know how many divs will occur after the input field is there any way to CUT or SLICE all the divs or any elements that occur after the second child of the Control DIV...

    The answer is yes, for this you want the :gt: selector: http://api.jquery.com/gt-selector/

    $('.control div:gt(1)').remove()
    

    As opposed to :nth-child, :gt uses zero-based indexing, so the first element has index 0.

提交回复
热议问题