我想从表中删除除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>
但是下面的JQuery代码删除了所有行:
$('.mytable').children().not('#row0').remove();
有人能解释一下为什么会发生这种情况吗?我会认为id为'row0‘的孩子会被排除在外,但显然情况并非如此。
我已经找到了另一种方法来做这件事,但我仍然很好奇为什么上面的方法不起作用:
$('.mytable').find('tr:not(#row0)').remove();
发布于 2011-04-16 09:34:15
因为table
元素的子元素是thead
、tfoot
或tbody
元素。A tbody
element is always created in the generated DOM,即使它不是用HTML码显式编写的。
您还可以执行以下操作:
$('.mytable tr').not('#row0').remove();
或
$('#row0').siblings().remove();
https://stackoverflow.com/questions/5683859
复制相似问题