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
Release: 2023-01-04 09:38:10
Original
6060 people have browsed it

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 <div class="code" style="position:relative; padding:0px; margin:0px;"><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><div class="contentsignin">Copy after login</div></div>2. The hasAttribute method of native js

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

3. Filter selection Way

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template