jQuery中:first-child和:first-of-type的区别

:first-child和:first-of-type的区别:

//选择一个 是所有兄弟元素里面第一个并且是li元素
$("li:first-child").css('border','2px solid red');
//所有li兄弟元素里面的第一个
$("li:first-of-type").css('border','2px solid pink');

以下示例:

<ul>
        <li>123</li>
        <li>lorem
            <ol>
                <p>loersda</p>
                <li>456</li>
                <li>lorem5</li>
                <li>lorem5</li>
                <p>lasdasd</p>
                <li>lorem5</li>
                <p>sadadasd</p>
                <li>lorem5</li>
            </ol>
        </li>
        <li>lorem</li>
        <li>lorem</li>
        <li>lorem</li>
    </ul>
    <ul>
        <li>789</li>
    </ul>
  • :first-child
$("li:first-child").css('border','2px solid red');

  • :first-of-type
$("li:first-of-type").css('border','2px solid pink');

全部评论

相关推荐

03-12 12:33
嘉应学院 Python
堆肥大王:认可你的做法,但无产阶级的兄弟们也希望你能过的更好
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
04-23 10:48
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务