Home > Web Front-end > JS Tutorial > How to modify the style attribute in jquery

How to modify the style attribute in jquery

青灯夜游
Release: 2021-11-12 16:45:47
Original
19051 people have browsed it

You can use the attr() method in jquery to modify the style attribute. This method sets the attribute value of the selected element. The syntax is "$(selector).attr("style","inline style code")" or "$(selector).attr({"style":"style code"})".

How to modify the style 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 style attribute of an element.

attr() method can set the attribute value of the selected element. Depending on the parameters of the method, the way it works is also different.

Grammar:

$(selector).attr(attribute,value)
$(selector).attr({attribute:value})
Copy after login

Example:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
	$("button").click(function(){
		// $("div").attr({"style":"border: 5px solid paleturquoise;"});
		$("div").attr("style","border: 5px solid paleturquoise;width: 200px;");
	});
});
</script>
</head>
<body>

<div style="border: 1px solid red;">hello</div>
<br>
<button>点击按钮,修改style属性</button>

</body>
</html>
Copy after login

How to modify the style attribute in jquery

Related video tutorial recommendation: jQuery tutorial( video)

The above is the detailed content of How to modify the style attribute 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