Home >Web Front-end >Front-end Q&A >How to modify the value of a specified attribute in jquery
In jquery, you can use the attr() method to modify the value of the specified attribute. This method can set the attribute value of the selected element. The syntax "$(selector).attr("Attribute name","Attribute Value ")" or "$(selector).attr({attribute name: attribute value})".
The operating environment of this tutorial: windows7 system, jquery1.10.2 version, Dell G3 computer.
In jquery, you can use the attr() method to modify the value of a specified attribute.
attr() method sets or returns the attribute value of the selected element. Depending on the parameters of the method, the way it works is also different.
The syntax for setting attribute values:
$(selector).attr(attribute,value) //单个属性/值对 $(selector).attr({attribute:value, attribute:value ...}) //多个属性/值对
Example:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script src="js/jquery-1.10.2.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ $("img").attr("width","380"); }); }); </script> </head> <body> <img src="img/1.jpg" style="max-width:90%" / alt="How to modify the value of a specified attribute in jquery" > <br /> <button>设置图像的 width 属性</button> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script src="js/jquery-1.10.2.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ $("img").attr({width:"300",height:"350"}); }); }); </script> </head> <body> <img src="img/1.jpg" style="max-width:90%" / alt="How to modify the value of a specified attribute in jquery" > <br /> <button>设置图像的 width 和 height 属性</button> </body> </html>
[Recommended learning: jQuery video tutorial, web front-end]
The above is the detailed content of How to modify the value of a specified attribute in jquery. For more information, please follow other related articles on the PHP Chinese website!