Home >Web Front-end >JS Tutorial >How to determine whether an attribute exists in jquery
Jquery method to determine whether an attribute exists: 1. Use [attr()] to obtain the attribute and check the type of the value; 2. The hasAttribute method of native js; 3. Filter selection method, the code is [$(this ).is('[name]');].
The operating environment of this tutorial: windows7 system, jquery3.2.1 version, DELL G3 computer.
Recommended: jquery video tutorial
##jquery method to determine whether an attribute exists:
jquery can use theattr() method,
is() method,
filter() method, and native js's ## to determine the existence of an attribute. #hasAttribute()
Method. 1. Use
to get the attribute and check the value type <pre class="brush:php;toolbar:false">var attr = $(this).attr(&#39;name&#39;);
// 对于一些浏览器,attr是undefined、或者是false
if (typeof attr !== typeof undefined && attr !== false) {
alert(&#39;具有该属性&#39;)
}</pre>
2. The hasAttribute method of native js
$(this)[0].hasAttribute("name"); jQObject[0].hasAttribute("name");
3. Filter selection Way
$(this).is('[name]'); $(this).filter("[name='choice']");
Related free learning recommendations:
The above is the detailed content of How to determine whether an attribute exists in jquery. For more information, please follow other related articles on the PHP Chinese website!