Jquery determines whether an attribute exists hasAttr

Reference   https://www.cnblogs.com/xuhongfei/p/3529704.html

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

But sometimes we need to judge other attributes, such as some a link contains rel attribute, and some do not have rel attribute. How to judge at this time?

At this time, there is no ready-made method. If there is an attribute $("#aid").attr("rel") will return the value of rel, if there is no rel attribute, it will return "undefined"

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

Because the types are not the same.

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

Guess you like

Origin blog.csdn.net/xushiyu1996818/article/details/114675129