删除表中除<th>标记之外的所有行

Dan*_*nka 5 jquery

如果我有id ="tbl_users"的表,我有

<tr>
   <th>name</th>
   <th>username</th>
   <th>password</th>
</tr>
Run Code Online (Sandbox Code Playgroud)

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

Guf*_*ffa 21

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

$('#tbl_users tr').slice(1).remove();
Run Code Online (Sandbox Code Playgroud)

否则你可以使用这个has方法:

$('#tbl_users tr').has('td').remove();
Run Code Online (Sandbox Code Playgroud)

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

$('#tbl_users tr').filter(function() {
  return !$(this).has('th');
}).remove();
Run Code Online (Sandbox Code Playgroud)


Viv*_*vek 7

tbody在你的HTML中添加标签,然后执行此操作 -

 $('#tbl_users').find('tbody').remove();
Run Code Online (Sandbox Code Playgroud)