jQuery层次选择器

<!--@description-->
<!--@author beyondx-->
<!--@date Created in 2022/08/01/ 9:45-->
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>jQuery层次选择器</title>
</head>
<body>
<p></p>
<div id="father">
    <div>
        <span>span1</span>
        <span>span2</span>
        <span>span3</span>
    </div>
    <div>
        <b>1</b>
        <div>div1</div>
        <div>div2</div>
        <div>div3</div>
        <p></p>
    </div>
    <div></div>
    <p></p>
    <p></p>
    <p></p>
    <span>sss1</span>
</div>
</body>
</html>
<script src="jquery-1.12.4.js"></script>
<script>
    $(function () {
        //需求1:获取id为father这个元素的所有子div.
        //   console.log($('#father > div'));

        //需求2:获取id为father这个元素的所有子div以及所有子p.
        // console.log($('#father > div, p'));
        // console.log($('#father > div, #father > p'));


        //需求3:获取id为father这个div的所有后代div.
        // console.log($('#father div'));


        //需求4:获取id为father这个div的所有后代div,以及id为father这个div的所有后代span.
        //   console.log($('#father div, #father div span')); // 错误, 显示9个
        console.log($('#father div, #father span')); // 显示 10个

        /**
         * jQuery层级选择器
         * 子代选择器: $('ul > li')
         * 藕带选择器: $('ul li'); // 包括孙子
         */

    });
</script>

在这里插入图片描述

posted on   beyondx  阅读(28)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示