jQuery 按属性选择元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1097522/
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
Select elements by attribute
提问by Ciprian Grosu
I have a collection of checkboxes with generated ids and some of them have an extra attribute. Is it possible to use JQuery to check if an element has a specific attribute? For example, can I verify if the following element has the attribute "myattr"? The value of the attribute can vary.
我有一组带有生成 ID 的复选框,其中一些具有额外的属性。是否可以使用 JQuery 来检查元素是否具有特定属性?例如,我能否验证以下元素是否具有“myattr”属性?该属性的值可能会有所不同。
<input type="checkbox" id="A" myattr="val_attr">A</input>
For example how can I get a collection of all checkboxes that have this attribute without checking one by one? Is this possible?
例如,如何在不一一检查的情况下获取具有此属性的所有复选框的集合?这可能吗?
采纳答案by cletus
Do you mean can you select them? If so, then yes:
你的意思是你可以选择它们吗?如果是这样,那么是:
$(":checkbox[myattr]")
回答by Stefan Gehrig
if ($('#A').attr('myattr')) {
// attribute exists
} else {
// attribute does not exist
}
EDIT:
编辑:
The above will fall into the else
-branch when myattr
exists but is an empty string or "0". If that's a problem you should explicitly test on undefined
:
else
当myattr
存在但为空字符串或“0”时,上述内容将落入-branch 。如果这是一个问题,您应该明确测试undefined
:
if ($('#A').attr('myattr') !== undefined) {
// attribute exists
} else {
// attribute does not exist
}
回答by Jonathan Bergeron
I know it's been a long time since the question was asked, but I found the check to be clearer like this :
我知道自从提出这个问题已经很长时间了,但我发现支票更清晰,如下所示:
if ($("#A").is('[myattr]')) {
// attribute exists
} else {
// attribute does not exist
}
(As found on this site here)
(如在本网站上找到的)
Documentation about iscan be found here
可以在此处找到有关is 的文档
回答by powermikee
This will work:
这将起作用:
$('#A')[0].hasAttribute('myattr');
回答by bambams
In JavaScript,...
在 JavaScript 中,...
null == undefined
...returns true
*. It's the difference between ==
and ===
. Also, the name undefined
can be defined (it's not a keyword like null
is) so you're better off checking some other way. The most reliable way is probably to compare the return value of the typeof
operator.
...返回true
*。这之间的区别==
和===
。此外,undefined
可以定义名称(它不是像null
is 这样的关键字),因此您最好通过其他方式进行检查。最可靠的方法可能是typeof
比较运算符的返回值。
typeof o == "undefined"
Nevertheless, comparing to null should work in this case.
尽管如此,在这种情况下,与 null 相比应该有效。
* Assuming undefined
is in fact undefined.
* 假设undefined
实际上是未定义的。
回答by ssps
$("input[attr]").length
might be a better option.
$("input[attr]").length
可能是更好的选择。
回答by Daved
A couple ideas were tossed around using "typeof", jQuery ".is" and ".filter" so I thought I would post up a quick perf compare of them. The typeof appears to be the best choice for this. While the others will work, there appears to be a clear performance difference when invoking the jq library for this effort.
使用“typeof”、jQuery“.is”和“.filter”提出了一些想法,所以我想我会发布它们的快速性能比较。typeof 似乎是最好的选择。虽然其他人可以工作,但在为此工作调用 jq 库时似乎存在明显的性能差异。
回答by Fury
if (!$("#element").attr('my_attr')){
//return false
//attribute doesn't exists
}