删除表中的所有行,但带有标记的行除外

时间:2011-06-20 06:19:47

标签: jquery

如果我有id =“tbl_users”的表,我有

<tr>
   <th>name</th>
   <th>username</th>
   <th>password</th>
</tr>

然后我追加数据。如何在重新加载用户之前清空表(用darta删除所有行),而不是删除行?我在项目中使用JQuery。

3 个答案:

答案 0 :(得分:21)

如果您始终首先使用标题行,则可以跳过它:

$('#tbl_users tr').slice(1).remove();

否则您可以使用has方法:

$('#tbl_users tr').has('td').remove();

要专门查找没有th标记的行,您可以使用filter方法:

$('#tbl_users tr').filter(function() {
  return !$(this).has('th');
}).remove();

答案 1 :(得分:7)

在html中添加tbody标记,然后执行此操作 -

 $('#tbl_users').find('tbody').remove();

答案 2 :(得分:1)

$('table#tbl_users').find('tr:not(:has(th))).remove();
相关问题