jQuery delete all table rows except first

后端 未结 14 1177
失恋的感觉
失恋的感觉 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:04

    Your selector doesn't need to be inside your remove.

    It should look something like:

    $("#tableID tr:gt(0)").remove();
    

    Which means select every row except the first in the table with ID of tableID and remove them from the DOM.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-29 18:06

    -Sorry this is very late reply.

    The easiest way i have found to delete any row (and all other rows through iteration) is this

    $('#rowid','#tableid').remove();

    The rest is easy.

    0 讨论(0)
  • 2021-01-29 18:07

    This worked in the following way in my case and working fine

    $("#compositeTable").find("tr:gt(1)").remove();
    
    0 讨论(0)
  • 2021-01-29 18:11

    Consider a table with id tbl: the jQuery code would be:

    $('#tbl tr:not(:first)').remove();
    
    0 讨论(0)
  • 2021-01-29 18:12

    This should work:

    $(document).ready(function() {
       $("someTableSelector").find("tr:gt(0)").remove();
    });
    
    0 讨论(0)
提交回复
热议问题