Home >Web Front-end >JS Tutorial >How to determine whether an attribute exists in jquery

How to determine whether an attribute exists in jquery

coldplay.xixi
coldplay.xixiOriginal
2020-12-25 14:28:006026browse

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]');].

How to determine whether an attribute exists in jquery

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 the

attr() method, is() method, filter() method, and native js's ## to determine the existence of an attribute. #hasAttribute()Method. 1. Use

attr()

to get the attribute and check the value type <pre class="brush:php;toolbar:false">var attr = $(this).attr(&amp;#39;name&amp;#39;); // 对于一些浏览器,attr是undefined、或者是false if (typeof attr !== typeof undefined &amp;&amp; attr !== false) { alert(&amp;#39;具有该属性&amp;#39;) }</pre>2. The hasAttribute method of native js

$(this)[0].hasAttribute("name");
jQObject[0].hasAttribute("name");

3. Filter selection Way

$(this).is(&#39;[name]&#39;);
$(this).filter("[name=&#39;choice&#39;]");

Related free learning recommendations:

js video tutorial

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn