JQuery子元素过滤选择器

博客展示了JQuery子元素过滤选择器的示例及运行结果,聚焦于JQuery在前端开发中对元素选择和过滤的应用。
NO.选择器描述
1p:nth-child(n|even|odd)属于其父元素的第n个|第偶数个|第奇数个子元素且为p的元素集合,从1开始
2p:first-child属于其父元素的第一个子元素且为p的元素集合
3p:first-of-type属于其父元素的第一个为p的子元素的元素集合
4p:last-child属于其父元素的最后一个子元素且为p的元素集合
5p:last-of-type属于其父元素的最后一个为p的子元素的元素集合
6p:only-child属于其父元素的唯一子元素且为p的元素集合

示例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>

<body >

<div>hello</div>
<p id="p1"></p>
<p id="p2"></p>
<div>world</div>

<div>
    <p id="p3"></p>
    <p id="p4"></p>
    <div></div>
    <p id="p5"></p>
</div>

<div>
    <div id="d1"></div>
</div>

<script src="http://apps.bdimg.com/libs/jquery/1.6.4/jquery.js"></script>
<script>
    var v1=$("p:nth-child(3)")
    var v2=$("p:nth-child(even)")
    var v3=$("p:nth-child(odd)")

    var v4=$("p:first-child")
    var v5=$("p:first-of-type")
    var v6=$("p:last-child")
    var v7=$("p:last-of-type")

    var v8=$("div:only-child")

    console.log(v1)
    console.log(v2)
    console.log(v3)
    console.log(v4)
    console.log(v5)
    console.log(v6)
    console.log(v7)
    console.log(v8)

</script>
</body>

</html>

运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值