Home >Web Front-end >Front-end Q&A >How to modify the value of a specified attribute in jquery

How to modify the value of a specified attribute in jquery

青灯夜游
青灯夜游Original
2022-03-01 16:18:277452browse

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

How to modify the value of a specified attribute in jquery

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>

How to modify the value of a specified attribute in jquery

<!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>

How to modify the value of a specified attribute in jquery

[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!

Statement:
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