Home >Web Front-end >JS Tutorial >How does jquery determine whether a certain attribute exists hasAttr

How does jquery determine whether a certain attribute exists hasAttr

coldplay.xixi
coldplay.xixiOriginal
2020-12-04 14:14:252347browse

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.

How does jquery determine whether a certain attribute exists hasAttr

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"

will be returned.

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!

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