clear table jquery

2020-05-11 21:05发布

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

How can I remove all the rows from the table?

10条回答
一纸荒年 Trace。
2楼-- · 2020-05-11 21:48

If you want to clear the data but keep the headers:

$('#myTableId tbody').empty();

The table must be formatted in this manner:

<table id="myTableId">
    <thead>
        <tr>
            <th>header1</th><th>header2</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>data1</td><td>data2</td>
        </tr>
    </tbody>
</table>
查看更多
Melony?
3楼-- · 2020-05-11 21:49

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

$("#tableLoanInfos tbody tr").remove();
查看更多
Evening l夕情丶
4楼-- · 2020-05-11 21:52
$("#employeeTable td").parent().remove();

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

查看更多
女痞
5楼-- · 2020-05-11 21:59
  $('#myTable > tr').remove();
查看更多
登录 后发表回答