Delete all rows from table except one with <th> tags
If I have table with id="tbl_users" and I have
<tr>
<th>name</th>
<th>username</th>
<th>password</th>
</tr>
and bellow I append data. How to empty table ( delete all rows with darta) before reloading users, but not to 开发者_StackOverflowdelete row with ? I use JQuery in project.
If you always have the header line first, you can simply skip it:
$('#tbl_users tr').slice(1).remove();
Otherwise you can use the has
method:
$('#tbl_users tr').has('td').remove();
To specifically look for rows that doesn't have a th
tag, you can use the filter
method:
$('#tbl_users tr').filter(function() {
return !$(this).has('th');
}).remove();
add tbody
tag in your html and then do this-
$('#tbl_users').find('tbody').remove();
$('table#tbl_users').find('tr:not(:has(th))).remove();
use this
$('#tbl_users td').parent().remove();
精彩评论