可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
<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?
回答1:
Do you mean can you select them? If so, then yes:
$(":checkbox[myattr]")
回答2:
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
:
if ($('#A').attr('myattr') !== undefined) {
// attribute exists
} else {
// attribute does not exist
}
回答3:
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 is can be found here
回答4:
In 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.
typeof o == "undefined"
Nevertheless, comparing to null should work in this case.
* Assuming undefined
is in fact undefined.
回答5:
This will work:
$('#A')[0].hasAttribute('myattr');
回答6:
$("input[attr]").length
might be a better option.
回答7:
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.
回答8:
$("input#A").attr("myattr") == null
回答9:
simply:
$('input[name*="value"]')
more info: official docs
回答10:
if (!$("#element").attr('my_attr')){
//return false
//attribute doesn't exists
}
回答11:
In addition to selecting all elements with an attribute $('[someAttribute]')
or $('input[someAttribute]')
you can also use a function for doing boolean checks on an object such as in a click handler:
if(! this.hasAttribute('myattr') ) { ...
回答12:
I have created npm package with intended behaviour as described above in question.
Link to [npm] and [github]
Usage is very simple. For example:
<p id="test" class="test">something</p>
$("#test").hasAttr("class")
returns true.
Works with camelcase too.
回答13:
JQuery will return the attribute as a string. Therefore you can check the length of that string to determine if is set:
if ($("input#A").attr("myattr").length == 0)
return null;
else
return $("input#A").attr("myattr");