Home >Web Front-end >JS Tutorial >How does jquery determine whether a certain attribute exists hasAttr
Jquery method to determine whether hasAttr exists for an attribute: Use [if(typeof($("#aid").attr("rel"))=="undefined")] to determine whether it exists A certain attribute [$("#aid").attr("rel")] will return the value of rel.
The operating environment of this tutorial: windows7 system, jquery3.2.1 version. This method is suitable for all brands of computers.
Jquery's method of determining whether an attribute exists hasAttr:
In JQuery coding, we will determine whether an element exists or not. For example, whether it contains class=" new" style. JQuery judgment is very simple,
because there is the hasClass method $("input[name=new]").hasClass("new")
That's it Judgment.
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 such thing at this time A ready-made method. If there is an attribute $("#aid").attr("rel")
, the value of rel will be returned. 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 type is not Same.
It is recommended to useif(typeof($("#aid").attr("rel"))=="undefined")
That's it
Related learning recommendations: javascript video tutorial
The above is the detailed content of How does jquery determine whether a certain attribute exists hasAttr. For more information, please follow other related articles on the PHP Chinese website!