Home > Article > Web Front-end > What does attr mean in jquery
In jquery, attr is a method used to set or return the attributes and values of the selected element; when this method is used to return the attribute value, it can return the value of the first matching element; this method is used When setting attribute values, you can set multiple attribute values. The syntax is "element object.attr(attribute)" or "element object.attr(attribute, attribute value)".
The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.
attr() method sets or returns the attributes and values of the selected element.
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)
Use the function to set the attribute and value:
$(selector).attr(attribute,function(index,currentvalue))
Set multiple attributes and values:
$(selector).attr({attribute:value, attribute:value,...})
Examples of returned attribute values are as follows:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>123</title> <script src="js/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ alert("图片宽度: " + $("img").attr("width")); }); }); </script> </head> <body> <img src="img_pulpitrock.jpg" alt="Pulpit Rock" width="284" style="max-width:90%"> <br> <button>返回图片的宽度</button> </body> </html>
Output results;
##Click After the button: Recommended video tutorial:The above is the detailed content of What does attr mean in jquery. For more information, please follow other related articles on the PHP Chinese website!