jQuery delete all table rows except first

后端 未结 14 1225
失恋的感觉
失恋的感觉 2021-01-29 17:40

Using jQuery, how do I delete all rows in a table except the first? This is my first attempt at using index selectors. If I understand the examples correctly, the following sh

14条回答
  •  臣服心动
    2021-01-29 18:06

    I think this is more readable given the intent:

    $('someTableSelector').children( 'tr:not(:first)' ).remove();
    

    Using children also takes care of the case where the first row contains a table by limiting the depth of the search.

    If you had an TBODY element, you can do this:

    $("someTableSelector > tbody:last").children().remove();
    

    If you have THEAD or TFOOT elements you'll need to do something different.

提交回复
热议问题