clear table jquery

后端 未结 10 1548
耶瑟儿~
耶瑟儿~ 2021-01-30 05:03

I have an HTML table filled with a number of rows.

How can I remove all the rows from the table?

相关标签:
10条回答
  • 2021-01-30 05:16

    I needed this:

    $('#myTable tbody > tr').remove();
    

    It deletes all rows except the header.

    0 讨论(0)
  • 2021-01-30 05:16
    <table id="myTable" class="table" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>Header 1</th>
                <th>Header 2</th>
                <th>Header 3</th>
            </tr>
        </thead>
        <tbody id="tblBody">
    
        </tbody>
    </table>
    

    And Remove:

    $("#tblBody").empty();
    
    0 讨论(0)
  • 2021-01-30 05:16

    Having a table like this (with a header and a body)

    <table id="myTableId">
        <thead>
        </thead>
        <tbody>
       </tbody>
    </table>
    

    remove every tr having a parent called tbody inside the #tableId

    $('#tableId tbody > tr').remove();
    

    and in reverse if you want to add to your table

    $('#tableId tbody').append("<tr><td></td>....</tr>");
    
    0 讨论(0)
  • 2021-01-30 05:18

    This will remove all of the rows belonging to the body, thus keeping the headers and body intact:

    $("#tableLoanInfos tbody tr").remove();
    
    0 讨论(0)
  • 2021-01-30 05:25

    Use .remove()

    $("#yourtableid tr").remove();
    

    If you want to keep the data for future use even after removing it then you can use .detach()

    $("#yourtableid tr").detach();
    

    If the rows are children of the table then you can use child selector instead of descendant selector, like

    $("#yourtableid > tr").remove();
    
    0 讨论(0)
  • 2021-01-30 05:26
    $("#employeeTable td").parent().remove();
    

    This will remove all tr having td as child. i.e all rows except the header will be deleted.

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