Home > Web Front-end > Front-end Q&A > How to change the attribute value of img in jquery

How to change the attribute value of img in jquery

青灯夜游
Release: 2022-05-25 15:30:40
Original
2770 people have browsed it

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"...});".

How to change the attribute value of img in jquery

The operating environment of this tutorial: windows7 system, jquery1.10.2 version, Dell G3 computer. The

How to change the attribute value of img in jquery tag defines an image in an HTML page.

<img src="img/1.jpg" alt="多肉" width="200">
Copy after login

How to change the attribute value of img in jquery

How to change the attribute value of img in jquery 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:"属性值"....});
Copy after login

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>
Copy after login

How to change the attribute value of img in jquery

Method 2: Use prop() to modify attribute values ​​

Modify syntax:

//单个属性
$("img").prop("属性名","属性值");
//多个个属性
$("img").prop({属性1:"属性值",属性2:"属性值"....});
Copy after login

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>
Copy after login

How to change the attribute value of img in jquery

Extended knowledge: How to change the attribute value of img in jquery Attributes that can be set

srcSpecifies the URL to display the image. usemap Defines the image as a client-side image map. vspacewidthSpecifies the width of the image.
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
#mapname
pixels HTML5 Not supported. HTML 4.01 is deprecated. Specifies the margin at the top and bottom of the image.
pixels
[Recommended learning:

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template