jQuery子元素过滤器的使用方式及含义

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<ul>
		<li></li>
		<li></li>
		<li></li>
		<li></li>
		<li></li>
		<li></li>
	</ul>
	<ul>
		<li></li>
		<li></li>
		<li></li>
		<li></li>
		<li></li>
		<li></li>
	</ul>
	<ul>
		<li></li>
	</ul>
	<ul>
		<li></li>
	</ul>
</body>
<script type="text/javascript" src="jquery-3.3.1.min.js"></script>
<script type="text/javascript">
$("ul li:first-child").css("background","red") 
// :first-child 选取父元素内的第一个子元素

$("ul li:first-of-type").css("background","pink")
// :first-of-type 查找父元素ul的子元素中的第一个li标签

$("ul li:last-child").css("background","red")
// :last-child 选取父元素内的最后一个子元素

$("ul li:last-of-type").css("background","pink")
// :last-of-type 查找父元素ul的子元素中的最后一个li标签

$("ul li:nth-child(2)").css("background","blue")
// :nth-child(2) 选取父元素内正数第二个子元素,child()括号内写几就是正数第几个子元素

$("ul li:nth-last-child(2)").css("background","black")
// :nth-last-child(2) 选取父元素内倒数第二个子元素,child()括号内写几就是倒数第几个子元素

$("ul li:nth-last-of-type(3)").css("background","green")
// :nth-last-of-type(3) 查找父元素ul的子元素中倒数第三个li

$("ul li:nth-last-of-type(3n-2)").css("background","green")
// :nth-last-of-type(3n-2) 查找父元素ul的子元素中倒数顺序符合type()括号内值的li

$("ul li:nth-of-type(2n)").css("background","#009494")
// :nth-of-type(2n) 查找父元素ul的子元素中正数顺序符合type()括号内值的li

$("ul li:only-child").css("background","#445845")
// :only-child 选取父元素ul中只有一个子元素li的

$("ul li:only-of-type").css("background","#235485")
// :only-of-type 查找父元素ul内只有一个子元素li的,如果ul内li有兄弟元素则不匹配
</script>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值