Home > Web Front-end > JS Tutorial > body text

How to determine whether an element has a certain attribute in jquery

青灯夜游
Release: 2021-11-15 17:10:32
Original
3888 people have browsed it

In jquery, you can use the attr() method to determine whether an element has an attribute. The syntax is "$(selector).attr(attrName)"; if there is an attrName attribute, the value of attrName will be returned. If the property does not exist, "undefined" will be returned.

How to determine whether an element has a certain attribute in jquery

The operating environment of this tutorial: windows7 system, jquery1.10.2 version, Dell G3 computer.

In jquery, you can use the attr() method to determine whether an element has an attribute.

attr() method can return the attribute value of the selected element. Syntax:

$(selector).attr(attrName);
Copy after login

If an attrName attribute exists, the value of attrName will be returned. If the attribute does not exist, "undefined" will be returned. (undefined is the undefined type).

Therefore, to judge when to use attributes, you can judge based on the results:

if(typeof($(selector).attr(attrName))=="undefined"){
	console.log("属性不存在");
}
else{
	console.log("属性存在");
}
Copy after login

Recommended related tutorials: jQuery video tutorial

The above is the detailed content of How to determine whether an element has a certain attribute in jquery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template