我想删除表中id为'row0'的行以外的所有行:
<table class="mytable">
<tr id="row0" class="myrow">
<td>aaa</td>
</tr>
<tr class="myrow">
<td>bbb</td>
</tr>
<tr class="myrow">
<td>ccc</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
但是以下JQuery代码删除了所有行:
$('.mytable').children().not('#row0').remove();
Run Code Online (Sandbox Code Playgroud)
有人能解释为什么会这样吗?我认为id为'row0'的孩子会被排除在外,但显然情况并非如此.
我找到了另一种方法来做到这一点,但仍然好奇为什么上述方法不起作用:
$('.mytable').find('tr:not(#row0)').remove();
Run Code Online (Sandbox Code Playgroud)