我想选择一个表的第一行 . 注意:第一行包含在 thead
标记中,因此:
<table>
<thead>
<tr> <!-- first row --> </tr>
</thead>
<tbody>
<tr> <!-- body --> </tr>
</tbody>
<tfoot>
<tr> <!-- body --> </tr>
</tfoot>
</table>
'select first row'提示可以工作,如果它不是't for the wrapper tags. This is what I have tried but doesn'工作:
$("*:not(thead)", tableSelector).remove();
即,我想使用"not tfoot"选择器摆脱tbody和tfoot选择器 . 因为除了 <thead>
和内部的所有内容之外,我想从表中删除其他所有内容 . 所以基本上我要做的就是选择除了thead之外的所有东西以及它里面的东西;像 :not(thead *)
这样的东西可以起作用,但不行 .
我的解决方法是 $("tbody, tfoot", tableSelector).remove();
但我想学习并理解如何使用相反的(非选择器) .
6 回答
“:not”伪选择器接收另一个选择器作为参数,因此您实际上可以删除除此之外的所有内容,如下所示:
你可以see it in action here .
这与您在your comment中所写的内容非常相似,除非您在thead中没有't use a context (which removed the table node) and you used the children operand which didn'包含"everything",而是仅包含它's direct children. Removing the children operand 2373022 excludes all the node'的后代,而不仅仅是直接子项 .
我会像:
你的问题不是很清楚 . 要选择单个表中的第一行:
或者相反:
如果有多个表(即使有三个表,它只会选择一行),它会这样做但会破坏 . 你可以解决这个问题:
你可以获得不在THEAD中的所有行:
Edit: 我认为你're over-complicating this. If you want to remove everything except THEAD and its contents, that'相对简单:
如果要将TBODY和TFOOT元素保留在原位,请将其更改为:
在表格选择器上使用children()将仅选择直接子节点 . 您可以使用非选择器过滤此内容 .
为什么不使用好的旧DOM呢?
如果要删除除
thead
之外的任何child oftable
,请尝试使用此选择器:Edit 由于您指出您已在变量中拥有该表: