比较jQuery:nth-of-type() 选择器和:nth-child()选择器

1,说明:type : 类型
child : 孩子
代码说明:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>选择器比较</title>
    <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js" type="text/javascript" charset="utf-8"></script>
    <script>
        $(function() {
                    var ys1 = $('li:nth-child(2)');
                    ys1.css({
                        width: '100px',
                        height: '100px',
                        backgroundColor: 'red',
                    });
                }
    </script>
</head>

<body>
    <ul>
        <li>01</li>
        <span>02</span>
        <li>03</li>
        <li>04</li>
    </ul>
</body>

</html>

运行结果:

在这里插入图片描述

 <script>
        $(function() {
                    var ys1 = $('li:nth-of-type(2)');
                    ys1.css({
                        width: '100px',
                        height: '100px',
                        backgroundColor: 'red',
                    });
                }

运行结果:
在这里插入图片描述
总结: :nth-child()选择器,若第n个孩子不是指定的元素,则不会被选中。
nth-of-type()选择器,是在指定的类型中选取。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值