Removing children elements given an index?

后端 未结 3 1146
刺人心
刺人心 2020-12-30 03:29

I\'ve got a layout like this:

相关标签:
3条回答
  • 2020-12-30 03:59

    This is an old thread, but I'm curious* why nobody mentioned nth-child.

    $("#parent > div:nth-child(n + " + index + ")").remove();
    

    *Update: I didn't have enough rep to know it at the time, but there had been an nth-child answer, deleted.

    0 讨论(0)
  • 2020-12-30 04:16

    To remove rows 0 and 1 select rows less than 2 using the lt selector and then remove them:

    $('#parent div:lt(2)').remove();
    
    0 讨论(0)
  • 2020-12-30 04:20

    "Just remove (detach) all children of #parent, starting at element N":

    $("#parent").children().slice(N).detach();
    

    If the elements are not going to be reinserted, use remove() instead of detach() in order to discard data and events associated with the removed elements.

    0 讨论(0)
提交回复
热议问题