jQuery“选择所有除外”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1896489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery "select all except"
提问by Martin
I am trying to select the first row of a table. Note: the first row is wrapped in a thead
tag, so:
我正在尝试选择表格的第一行。注意:第一行被包裹在一个thead
标签中,所以:
<table>
<thead>
<tr> <!-- first row --> </tr>
</thead>
<tbody>
<tr> <!-- body --> </tr>
</tbody>
<tfoot>
<tr> <!-- body --> </tr>
</tfoot>
</table>
The 'select first row'tip could work if it wasn't for the wrapper tags. This is what I have tried but doesn't work:
如果不是包装标签,“选择第一行”提示可能会起作用。这是我尝试过但不起作用的方法:
$("*:not(thead)", tableSelector).remove();
I.e., I would like to get rid off both tbody and tfoot selectors using a "not tfoot" selector. Because I want to remove everything else from the table except the <thead>
and everything-inside-thead. So basically what I am trying to do is to select everything except thead and what's inside it; intutuively something like :not(thead *)
could work, but doesn't.
即,我想使用“not tfoot”选择器摆脱 tbody 和 tfoot 选择器。因为我想从表中删除除<thead>
thead 和所有内容之外的所有其他内容。所以基本上我想要做的是选择除了 thead 和里面的东西之外的所有东西;直觉上,类似的东西:not(thead *)
可以工作,但不行。
My workaround is $("tbody, tfoot", tableSelector).remove();
but I would like to learn and understand how to use the opposite (not-selector).
我的解决方法是$("tbody, tfoot", tableSelector).remove();
但我想学习和了解如何使用相反的(非选择器)。
回答by cletus
Your question isn't exactly clear. To select the first row in a single table:
你的问题不是很清楚。要选择单个表中的第一行:
$("table tr:first")...
or the reverse:
或相反:
$("table tr:not(:first)")...
will do it but that will break if there's more than one table (it will only select one row even if there are three tables). You can work around that with:
会这样做,但是如果有多个表(即使有三张表,它也只会选择一行),这会中断。你可以解决这个问题:
$("table").each(function() {
$(this).find("tr:first")...
});
You can get all rows not in THEAD with:
您可以使用以下命令获取不在 THEAD 中的所有行:
$("table > :not(thead) > tr")...
Edit:I think you're over-complicating this. If you want to remove everything except THEAD and its contents, that's relatively simple:
编辑:我认为你把这个问题复杂化了。如果要删除除 THEAD 及其内容之外的所有内容,则相对简单:
$("table > :not(thead)").remove();
If you want to leave the TBODY and TFOOT elements in place change it to:
如果您想保留 TBODY 和 TFOOT 元素,请将其更改为:
$("table > :not(thead) > tr").remove();
回答by tvanfosson
Using children()on the table selector will select only the direct children. You can filter this using your not selector.
在表选择器上使用children()将只选择直接子级。您可以使用 not 选择器过滤它。
$(tableSelector).children(':not(thead)').remove();
回答by bobince
I am trying to select the first row of a table.
我正在尝试选择表格的第一行。
Why not use good old DOM?
为什么不使用好的旧 DOM?
mytable.rows[0]
回答by Gumbo
回答by jacmkno
The ":not" pseudo-selector receives another selector as a parameter, so you can actually remove everything except this and that like this:
":not" 伪选择器接收另一个选择器作为参数,因此您实际上可以删除除此之外的所有内容,如下所示:
$(':not(thead, thead *)', 'table').remove();
You can see it in action here.
你可以在这里看到它的实际效果。
This is very similar to what you wrote in your comment, except you didn't use a context (which removed the table node) and you used the children operand which didn't include "everything" inside the thead, but instead included only it's direct children. Removing the children operand ">" excludes all the node's descendants, not only the direct children.
这与您在评论中所写的非常相似,除了您没有使用上下文(删除了表节点)并且您使用了子操作数,该操作数不包含在 thead 中的“所有内容”,而是仅包含它直接的孩子。删除子操作数 ">" 会排除节点的所有后代,而不仅仅是直接子节点。
回答by Daghash
I will be like:
我会像:
$('table thead tr:first-child')