Removing Children of DIV after certain Number

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

I have this HTML

相关标签:
5条回答
  • 2020-12-30 10:09

    You can do this

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

    Or you can do this as well

    $('.control div:nth-child(1)').siblings().remove();
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-30 10:12
    $('.controll>div:gt(1)').remove();
    

    :gt selector will let you select which has index greater then 1 theese are 3. elements and more

    here is example: http://jsfiddle.net/Am7Vw/1/

    0 讨论(0)
  • 2020-12-30 10:15

    If you mean those specific ones:

    $(".ui-resizable-handle, .delete").remove();
    

    If you mean the divs at positions 2-4 no matter the markup, one of the nth-child() answers would work.

    0 讨论(0)
  • 2020-12-30 10:26

    Try:

    
    $('.control').find("div").slice(1, 4).remove();
    
    
    0 讨论(0)
提交回复
热议问题