• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

jquery怎么改变style样式

武飞扬头像
PHP中文网
帮助582

jquery改变style样式有两种方法:

  • 通过css()方法来修改

  • 通过attr()方法来修改

方法1:使用css()方法

css() 方法可以设置匹配的元素的一个或多个样式属性。

直接利用css() 方法设置新style样式即可

语法:

$(selector).css({"属性名1":"属性值1","属性名2":"属性值2",...})

示例:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<script src="https://www.swvq.com/js/jquery-3.6.3.min.js"></script>
		<script>
			$(document).ready(function() {
				$("button").on("click", function() {
					$("div").css({"color":"red","background-color":"papayawhip","font-size":"25px"});
				});
			});
		</script>
		<style>
			.tr1 {
				color: green;
			}
			.tr2 {
				color: red;
				background-color: blanchedalmond;
			}
		</style>
	</head>

	<body class="ancestors">
		<div style="color: yellow;background-color:powderblue;">欢迎来到PHP中文网!</div><br>
		<button>改变div元素的style样式</button>
	</body>

</html>

学新通技术网

方法2:使用attr()方法

attr() 方法设置或返回被选元素的属性值。

只需要使用attr() 方法设置元素的style属性值即可。

语法:

$(selector).attr("style","属性名1:属性值1;属性名2:属性值3;...")

示例:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<script src="https://www.swvq.com/js/jquery-3.6.3.min.js"></script>
		<script>
			$(document).ready(function() {
				$("button").on("click", function() {
					$("div").attr("style", "color:pink;background-color:peru;font-size: 18px;");
				});
			});
		</script>
		<style>
			.tr1 {
				color: green;
			}
			.tr2 {
				color: red;
				background-color: blanchedalmond;
			}
		</style>
	</head>

	<body class="ancestors">
		<div style="color: red;background-color:papayawhip;">欢迎来到PHP中文网!</div><br>
		<button>改变div元素的style样式</button>
	</body>

</html>

学新通技术网

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanfgfjc
系列文章
更多 icon
同类精品
更多 icon
继续加载