我想从表中删除除 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”的 child 会被排除在外,但显然情况并非如此。
我已经找到了另一种方法来做到这一点,但仍然很好奇为什么上述方法不起作用:
$('.mytable').find('tr:not(#row0)').remove();
最佳答案
因为 table
元素的子元素是 thead
、tfoot
或 tbody
元素。 A tbody
element is always created in the generated DOM ,即使它没有明确写入 HTML 代码。
您还可以:
$('.mytable tr').not('#row0').remove();
或
$('#row0').siblings().remove();
关于javascript - 从表中删除除特定行以外的所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5683859/