jQuery delete all table rows except first

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

    Easiest way :

    $("#mytable").find($("tr")).slice(1).remove()
    

    -first reference the table
    -get the list of elements and slice it and remove the selected elements of the list

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

    wrapped in a function:

    function remove_rows(tablename) { 
        $(tablename).find("tr:gt(0)").remove();        
    }
    

    then call it:

    remove_rows('#table1');
    remove_rows('#table2');
    
    0 讨论(0)
提交回复
热议问题