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.
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!