jQuery"select all except"

Mar*_*tin 16 jquery jquery-selectors

我想选择一个表的第一行.注意:第一行包含在thead标记中,因此:

<table>
  <thead>
    <tr>  <!-- first row --> </tr>
  </thead>
  <tbody>
    <tr>  <!-- body --> </tr>
  </tbody>
  <tfoot>
    <tr>  <!-- body --> </tr>
  </tfoot>
</table> 
Run Code Online (Sandbox Code Playgroud)

如果不是包装器标签,"选择第一行"提示可以工作.这是我尝试但不起作用的:

 $("*:not(thead)", tableSelector).remove();
Run Code Online (Sandbox Code Playgroud)

也就是说,我想使用"非tfoot"选择器摆脱tbody和tfoot选择器.因为我想删除表中的所有其他内容,除了<thead>内部和内部的所有内容.所以基本上我要做的就是选择除了thead之外的所有东西以及它里面的东西; 一个类似的东西:not(thead *)可以工作,但不是.

我的解决方法是,$("tbody, tfoot", tableSelector).remove();但我想学习和理解如何使用相反的(非选择器).

cle*_*tus 20

你的问题不是很清楚.要选择单个表中的第一行:

$("table tr:first")...
Run Code Online (Sandbox Code Playgroud)

或者相反:

$("table tr:not(:first)")...
Run Code Online (Sandbox Code Playgroud)

如果有多个表(即使有三个表,它只会选择一行),它会这样做但会破坏.你可以解决这个问题:

$("table").each(function() {
  $(this).find("tr:first")...
});
Run Code Online (Sandbox Code Playgroud)

你可以获得不在THEAD中的所有行:

$("table > :not(thead) > tr")...
Run Code Online (Sandbox Code Playgroud)

编辑:我认为你过于复杂了.如果你想删除除THEAD及其内容以外的所有内容,那就相对简单了:

$("table > :not(thead)").remove();
Run Code Online (Sandbox Code Playgroud)

如果要将TBODY和TFOOT元素保留在原位,请将其更改为:

$("table > :not(thead) > tr").remove();
Run Code Online (Sandbox Code Playgroud)