公告
财富商城
积分规则
提问
发文
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?
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>");
Slightly quicker than removing each one individually:
$('#myTable').empty()
Technically, this will remove thead, tfoot and tbody elements too.
thead
tfoot
tbody
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();
I needed this:
$('#myTable tbody > tr').remove();
It deletes all rows except the header.
The nuclear option:
$("#yourtableid").html("");
Destroys everything inside of #yourtableid. Be careful with your selectors, as it will destroy any html in the selector you pass!
#yourtableid
<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();
最多设置5个标签!
Having a table like this (with a header and a body)
remove every tr having a parent called tbody inside the #tableId
and in reverse if you want to add to your table
Slightly quicker than removing each one individually:
Technically, this will remove
thead
,tfoot
andtbody
elements too.Use .remove()
If you want to keep the data for future use even after removing it then you can use .detach()
If the rows are children of the table then you can use child selector instead of descendant selector, like
I needed this:
It deletes all rows except the header.
The nuclear option:
Destroys everything inside of
#yourtableid
. Be careful with your selectors, as it will destroy any html in the selector you pass!And Remove: