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.
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!