Home > Article > Web Front-end > Summary of using attr() method in jQuery_jquery
Definition and usage
Theattr() 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.
Example 1
Set the attributes and values of the selected element.
<html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ $("img").attr("width","180"); }); }); </script> </head> <body> <img src="/i/eg_smile.gif" /> <br /> <button>设置图像的 width 属性</button> </body> </html>
Example 2
Returns the attribute value of the selected element.
<html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ alert("Image width " + $("img").attr("width")); }); }); </script> </head> <body> <img src="/i/eg_smile.gif" width="128" height="128" /> <br /> <button>返回图像的宽度</button> </body> </html>
The above is the entire content of this article, I hope you all like it.