使用jquery可不可以加样式

发布时间:2021-11-22 11:06:37 作者:小新
来源:亿速云 阅读:146

这篇文章给大家分享的是有关使用jquery可不可以加样式的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

用jquery可以加样式,方法:1、使用“$(元素).attr("style","行内样式代码")”语句;2、使用“$(元素).css({"属性名":"属性值"})”语句;3、使用“$(元素).addClass("class名称")”语句。

本教程操作环境:windows7系统、jquery1.10.2版本、Dell G3电脑。

在jquery中,有多种方法可以给元素添加样式,下面介绍几种:

方法1:使用attr()方法添加行内样式

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

使用jquery可不可以加样式

方法2:使用css()方法

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

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

使用jquery可不可以加样式

方法3:使用addClass方法

addClass() 方法向被选元素添加一个或多个类。

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

使用jquery可不可以加样式

感谢各位的阅读!关于“使用jquery可不可以加样式”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

推荐阅读:
  1. jquery 样式 css
  2. jquery怎么删除样式

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

jquery

上一篇:php正则如何去除js

下一篇:c语言怎么实现含递归清场版扫雷游戏

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》