Home >Web Front-end >JS Tutorial >Solution to use JQuery to determine whether hasAttr exists for an attribute_jquery

Solution to use JQuery to determine whether hasAttr exists for an attribute_jquery

WBOY
WBOYOriginal
2016-05-16 17:35:111394browse

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")

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