Home  >  Article  >  Web Front-end  >  How to determine if there is a certain attribute in jquery

How to determine if there is a certain attribute in jquery

藏色散人
藏色散人Original
2020-11-17 10:52:263268browse

Jquery method to determine whether there is a certain attribute: 1. Determine through the "hasClass("new")" method; 2. Use the statement "if(typeof($("#aid").attr( "rel"))=="undefined")" to judge.

How to determine if there is a certain attribute in jquery

Recommended: "javascript basic tutorial"

JQuery determines whether a certain attribute exists hasAttr

In JQuery coding, we will judge whether an element has a certain attribute. For example, whether it contains the style of class="new". JQuery judgment is very simple because there is the hasClass method$ ("input[name=new]").hasClass("new") You can judge.

But sometimes we need to judge other attributes. For example, some a links contain the rel attribute, and some There is no rel attribute. How to judge at this time?

There is no ready-made method at this time. If there is an attribute $("#aid").attr("rel") will return rel value, if the rel attribute does not exist, "undefined"

undefined is the undefined type, if($("#aid").attr("rel")=="undefined") this judgment may not be true .

Because the types are different.

It is recommended to use if(typeof($("#aid").attr("rel"))=="undefined") That’s it

The above is the detailed content of How to determine if there is a certain attribute 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