Home > Web Front-end > JS Tutorial > Can I add styles using jquery?

Can I add styles using jquery?

青灯夜游
Release: 2021-11-22 10:58:53
Original
2726 people have browsed it

You can add styles with jquery. Method: 1. Use the "$(element).attr("style","inline style code")" statement; 2. Use "$(element).css({ "Attribute name":"Attribute value"})" statement; 3. Use the "$(element).addClass("class name")" statement.

Can I add styles using jquery?

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

In jquery, there are many ways to add styles to elements. Here are a few:

Method 1: Use the attr() method to add inline styles

<!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(){
				$("p").attr("style","border: 2px solid green; background-color: #55aa7f;color: white;");
			  });
			});
		</script>
	</head>
	<body>
		<p>hello,测试文字</p>
		<button>添加样式</button>
	</body>
</html>
Copy after login

Can I add styles using jquery?

Method 2: Use the css() method

css() method to return or set one or more styles of the matching element 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(){
				$("p").css({"border": "1px solid red","background-color": "#FFC0CB","color": "white"});
			  });
			});
		</script>
	</head>
	<body>
		<p>hello,测试文字</p>
		<button>添加样式</button>
	</body>
</html>
Copy after login

Can I add styles using jquery?

Method 3: Use the addClass method

addClass() method to add one or more classes to the selected element.

<!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(){
				$("p").addClass("intro");
			  });
			});
		</script>
		<style type="text/css">
			.intro {
				font-size: 120%;
				color: red;
			}
		</style>
	</head>
	<body>
		<p>hello,测试文字</p>
		<button>添加样式</button>
	</body>
</html>
Copy after login

Can I add styles using jquery?

Recommended related video tutorials: jQuery Tutorial (Video)

The above is the detailed content of Can I add styles using 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