7种jQuery操作css2.1选择器方法,总有一款适合你!

本博文源于jquery基础,主要讲解如何用jquery操作css2.1选择器,一共有七种分别如下,并且每种配置测试案例,可copy体会!

$("#box")  //选择id为box的元素
$(".box") //选择class为box的元素
$("div") // 选择div标签
$("div ul li")//选择div标签后代ul的后代li元素
$(".box ul li.spec")
//选择.box后代ul的后代携带spec类的li元素
$(".box,.spec")
//选择class为spec的元素和class为box的元素
$("div ul *")//选择div标签后代ul的所有元素

选择id为box的元素

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			#box {
				width: 80px;
				height: 80px;
				background-color: grey;
			}
		</style>
	</head>
		
	<body>
			<div id = "box"></div>
		<script type="text/javascript" src="./jquery-3.5.1.min.js"></script>
		<script type="text/javascript">
			$("#box").css("backgroundColor","orange");
		</script>
	</body>
</html>

在这里插入图片描述

选择class为box的元素

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			.box {
				width: 80px;
				height: 80px;
				background-color: grey;
			}
		</style>
	</head>
		
	<body>
			<div class="box" id = "box"></div>
		<script type="text/javascript" src="./jquery-3.5.1.min.js"></script>
		<script type="text/javascript">
			$(".box").css("backgroundColor","blue");
		</script>
	</body>
</html>

在这里插入图片描述

选择div标签

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			div {
				width: 80px;
				height: 80px;
				background-color: grey;
			}
		</style>
	</head>
		
	<body>

			<div ></div>
		<script type="text/javascript" src="./jquery-3.5.1.min.js"></script>
		<script type="text/javascript">
			$("div").css("backgroundColor","red");
		</script>
	</body>
</html>

在这里插入图片描述

选择div标签后代ul的后代li元素

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			div ul li {
				width: 80px;
				height: 80px;
				background-color: grey;
				list-style: none;
			}
		</style>
	</head>
		
	<body>

			<div >
				<ul>
					<li></li>
				</ul>
			</div>
		<script type="text/javascript" src="./jquery-3.5.1.min.js"></script>
		<script type="text/javascript">
			$("div ul li").css("backgroundColor","blue");
		</script>
	</body>
</html>

在这里插入图片描述

选择.box后代ul的后代携带spec类的li元素

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
		.box ul li.spec {
				width: 80px;
				height: 80px;
				background-color: grey;
				list-style: none;
			}
		</style>
	</head>
		
	<body>

			<div class="box">
				<ul>
					<li class="spec"></li>
				</ul>
			</div>
		<script type="text/javascript" src="./jquery-3.5.1.min.js"></script>
		<script type="text/javascript">
			$(".box ul li.spec").css("backgroundColor","green");
		</script>
	</body>
</html>

在这里插入图片描述

选择class为spec的元素和class为box的元素

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>

		.box  {
				width: 160px;
				height: 160px;
				background-color: green;
		
			}
			.spec {
					width: 80px;
					height: 80px;
					background-color: grey;
					list-style: none;
				}
		</style>
	</head>
		
	<body>

			<div class="box">
				<ul>
					<li class="spec"></li>
				</ul>
			</div>
		<script type="text/javascript" src="./jquery-3.5.1.min.js"></script>
		<script type="text/javascript">
			$(".box,.spec").css("backgroundColor","blue");
		</script>
	</body>
</html>

变化前
在这里插入图片描述
jquery变化后
在这里插入图片描述

选择div标签后代ul的所有元素

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>


			.spec {
					width: 100px;
					height: 100px;
					background-color: grey;
					list-style: none;
					margin-top: 10px;
			
				}
		</style>
	</head>
		
	<body>

			<div class="box">
				<ul>
					<li class="spec">nihao</li>
					<li class="spec">nihao</li>
					<li class="spec">nihao</li>
					<li class="spec">nihao</li>

				</ul>
			</div>
		<script type="text/javascript" src="./jquery-3.5.1.min.js"></script>
		<script type="text/javascript">
			$("div ul *").css("backgroundColor","red");
		</script>
	</body>
</html>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值