Home  >  Article  >  Web Front-end  >  How to determine whether an element has a certain attribute in jquery

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

青灯夜游
青灯夜游Original
2021-11-15 17:10:323878browse

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

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("属性存在");
}

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!

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