Home > Article > Web Front-end > What is the usage of jquery attr
The function of jquery attr is to set or return the attribute value of the selected element. Its usage syntax is "$(selector).attr(attribute)", where the parameter attribute specifies the attribute whose value is to be obtained.
The operating environment of this article: windows7 system, jquery3.2.1 version, DELL G3 computer
#What is the usage of jquery attr?
attr() method sets or returns the attribute value of the selected element.
According to the different parameters of this method, its working method is also different.
Return attribute value
Return the attribute value of the selected element.
Syntax
$(selector).attr(attribute)
Parameters
attribute specifies the attribute whose value is to be obtained.
Set attributes/values
Set the attributes and values of the selected element.
Syntax
$(selector).attr(attribute,value)
Parameters
attribute specifies the name of the attribute.
value Specifies the value of the attribute.
Use functions to set attributes/values
Set the attributes and values of the selected element.
Syntax
$(selector).attr(attribute,function(index,oldvalue))
Parameters
attribute specifies the name of the attribute.
function(index,oldvalue)
Specifies the function that returns the attribute value.
This function can receive and use the index value of the selector and the current attribute value.
Set multiple attribute/value pairs
Set more than one attribute and value for the selected element.
Syntax
$(selector).attr({attribute:value, attribute:value ...})
Parameters
attribute:value specifies one or more attribute/value pairs.
Recommended learning: "jquery video tutorial"
The above is the detailed content of What is the usage of jquery attr. For more information, please follow other related articles on the PHP Chinese website!