Home > Article > Web Front-end > How to change the attribute value of img in jquery
Two ways to change: 1. Use attr() to modify the attribute value, the syntax is "$("img").attr({Attribute 1: "value", Attribute 2: "value"...} );". 2. Use prop() to modify the attribute value, the syntax is "$("img").prop({Attribute 1: "Value", Attribute 2: "Value"...});".
The operating environment of this tutorial: windows7 system, jquery1.10.2 version, Dell G3 computer. The
tag defines an image in an HTML page.
<img src="img/1.jpg" alt="多肉" width="200">
tags have two required attributes: src and alt. (The attributes supported by the img tag can be viewed below)
So how to change the attribute value of img using jquery? The following introduces two methods of modifying element attributes.
Method 1: Use attr() to modify the attribute value
Modification syntax:
//单个属性 $("img").attr("属性名","属性值"); //多个个属性 $("img").attr({属性1:"属性值",属性2:"属性值"....});
Example: Modify the width and height attributes
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="js/jquery-1.10.2.min.js"></script> <script> $(document).ready(function() { $("button").click(function() { $("img").attr({width:"400",height":"320"}); }); }); </script> </head> <body> <img src="img/1.jpg" alt="多肉" width="200" style="max-width:90%"><br><br> <button>修改width和height属性值</button> </body> </html>
Method 2: Use prop() to modify attribute values
Modify syntax:
//单个属性 $("img").prop("属性名","属性值"); //多个个属性 $("img").prop({属性1:"属性值",属性2:"属性值"....});
Example : Modify src and alt attributes
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="js/jquery-1.10.2.min.js"></script> <script> $(document).ready(function() { $("button").click(function() { $("img").prop({src:"img/4.jpg",alt:"晚霞"}); }); }); </script> </head> <body> <img src="img/1.jpg" alt="多肉" width="300"><br><br> <button>修改src和alt属性</button> </body> </html>
Extended knowledge: Attributes that can be set
Attribute | Value | Description |
---|---|---|
align | top bottom middle left right |
HTML5 is not supported. HTML 4.01 is deprecated. Specifies how images are arranged relative to surrounding text. |
loading | eager: Load immediately lazy: Lazy loading |
Specifies whether the browser should load the image immediately or lazily. |
alt | text | Specifies the alternative text for the image. |
border | pixels | HTML5 Not supported. HTML 4.01 is deprecated. Specifies the border around the image. |
crossorigin | anonymous use-credentials |
Set the cross-domain attributes of the image |
height | pixels | Specify the image's high. |
hspace | pixels | HTML5 Not supported. HTML 4.01 is deprecated. Specifies the margins on the left and right sides of the image. |
ismap | ismap | Specifies the image as a server-side image map. |
longdesc | URL | ##HTML5 Not supported. HTML 4.01 is deprecated. Points to a URL that contains a long image description document. |
URL | Specifies the URL to display the image.||
#mapname | Defines the image as a client-side image map.||
pixels | HTML5 Not supported. HTML 4.01 is deprecated. Specifies the margin at the top and bottom of the image. | |
pixels | Specifies the width of the image.
jQuery video tutorial, web front-end video】
The above is the detailed content of How to change the attribute value of img in jquery. For more information, please follow other related articles on the PHP Chinese website!