jQuery 如何通过jQuery获取没有特定属性的元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1073779/
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
How can I get the elements without a particular attribute by jQuery
提问by Billy
I know how to get elements with a particular attribute:
我知道如何获取具有特定属性的元素:
$("#para [attr_all]")
But how can I get the elements WITHOUT a particular attribute? I try
但是如何获得没有特定属性的元素?我试试
$("#para :not([attr_all])")
But it doesn't work. What is the correct way to do this?
但它不起作用。这样做的正确方法是什么?
Let me give an example:
让我举个例子吧:
<div id="para">
<input name="fname" optional="1">
<input name="lname">
<input name="email">
</div>
jQuery:
jQuery:
$("#para [optional]") // give me the fname element
$("#para :not([optional])") //give me the fname, lname, email (fname should not appear here)
采纳答案by Pim Jager
First thing that comes to my mind (maybe sub optimal) :
我想到的第一件事(可能是次优):
$('p').filter(function(){
return !$(this).attr('attr_all');
});
However p:not([attr_all])
should work, so I think something else is going on in your code.
但是p:not([attr_all])
应该可行,所以我认为您的代码中发生了其他事情。
回答by Frank DeRosa
If your code example is the exact code you're using, I think the problem is an errant space.
如果您的代码示例是您正在使用的确切代码,我认为问题是错误的空间。
$("#para :not([attr_all])")
should be
应该
$("#para:not([attr_all])")
If you leave a space in there, it selects descendantsof #para
.
如果你把在那里的空间,它选择的后代的#para
。
回答by bittersweetryan
A faster way to do this is to first get all the elements with the ID para (which is invalid HTML if there is more than one, consider using a class instead) and use the filter (or find depending on how the HTML is constructed) method to only get the elements without the attribute like so:
一种更快的方法是首先获取所有带有 ID para 的元素(如果有多个,这是无效的 HTML,请考虑使用类代替)并使用过滤器(或根据 HTML 的构造方式查找)方法只获取没有属性的元素,如下所示:
$('#para').filter(':not([attr_all])');
Doing this you are having jQuery do much less work because you are only looping through a smaller subset of elements to get those without the attribute. Doing it with a single selector statement, eg $("#para :not([attr_all])")
, will cause jQuery to get all elements without the attribute, then filter them for ones with the ID of para.
这样做你让 jQuery 做的工作少得多,因为你只是循环通过一个较小的元素子集来获取那些没有属性的元素。使用单个选择器语句执行此操作,例如$("#para :not([attr_all])")
,将导致 jQuery 获取所有没有该属性的元素,然后过滤它们以查找 ID 为 para 的元素。
回答by chaos
Your :not
really should work, though, which makes me suspect something else is going on.
:not
不过,您确实应该工作,这让我怀疑正在发生其他事情。
For a full list of attribute selectors, see the jQuery Selectors docs.
有关属性选择器的完整列表,请参阅jQuery 选择器文档。
回答by Tom Zaczkiewicz
Frank DeRosa's answer is about right, but since the OP does want to find the descendants, it's a bit off. Trying it myself, it seems like it should be working the way it is, but it's possible that whatever you're running doesn't like the ':' operator being used directly as a descendant. Try using
Frank DeRosa 的回答大致正确,但由于 OP 确实想找到后代,所以有点不对。自己尝试一下,它似乎应该按原样工作,但是您运行的任何内容都可能不喜欢将 ':' 运算符直接用作后代。尝试使用
$("#para input:not([optional])")
Would have left a comment but can't yet.
本来可以发表评论,但还不能。
回答by Marco Balestra
Looking at CSS3 selectors documentation the correct to select a node “node” without an attribute “attr” is:
查看 CSS3 选择器文档,正确选择没有属性“attr”的节点“node”是:
<node>:not(<node>[<attr>])
E.g.: p:not(p[align])
In your case $("#para *:not([optional])")
works, and could be more precise.
在您的情况下$("#para *:not([optional])")
有效,并且可能更精确。
The actual syntax of ":not" is:
“ :not”的实际语法是:
<selector1>:not(<selector2>)
There are twoselectors in this expression, which selects all the nodes matching seletor1then excludes all nodes matching selector2.
有2个选择器在该表达式中,其选择的所有节点匹配seletor1然后排除匹配所有节点选择器2。
I would use:
我会用:
$('#para input:not(input[optional])')
/* or */
$('input:not(input[optional])',$('#para'))
/* in case jQuery has issues with :not */
$( document.querySelectorAll('#para input:not(input[optional])') )