Home>Article>Web Front-end> Can jquery add new attributes to elements?
jquery can add new attributes to elements; jquery can use the attr() method to add new attributes to elements. This method is used to set or return the attributes and values of the specified element. The first parameter in brackets is set For a newly added attribute, just set the second parameter to the value of the new attribute, and the syntax is "$(selector).attr(attribute,value)".
The operating environment of this tutorial: windows10 system, jquery3.6.0 version, Dell G3 computer.
jquery can add new attributes to elements
attr() method sets or returns the attribute of the selected element Properties and values.
When this method is used to return an attribute value, the value of the first matching element is returned.
When this method is used to set attribute values, one or more attribute/value pairs are set for the matching element.
Syntax
Return the value of the attribute:
$(selector).attr(attribute)
Set the attribute and value:
$(selector).attr(attribute,value)
The example is as follows:
123
Output result:
Video tutorial recommendation:jQuery video tutorial
The above is the detailed content of Can jquery add new attributes to elements?. For more information, please follow other related articles on the PHP Chinese website!