Home > Article > Web Front-end > Solution to use JQuery to determine whether hasAttr exists for an attribute_jquery
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") can be used to determine.
But sometimes we need to judge other attributes. For example, some a links contain the rel attribute, and some do not. 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 the value of rel, if the rel attribute does not exist, it will return "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")