Sejinda 2022-11-29 18:58 采纳率: 90%
浏览 6
已结题

jQuery中,children()方法作用于所有后代

问题遇到的现象和发生背景

jQuery中,children()方法作用于所有后代

用代码块功能插入代码,请勿粘贴截图

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>筛选方法</title>
    <script src="../jquery.min.js/jquery.min.js"></script>
    <style>
        .grandfather{
            width: 200px;
            height: 200px;
            border: 1px solid red ;
        }
        .father{
            width: 100px;
            height: 200px;
            border: 1px solid;
        }
        .son1{
            width: 50px;
            height: 70px;
            border: 1px solid;
        }
    </style>
</head>
<body>
    <div class="grandfather">
        <div class="father">
            <div class="son1">son1
                <div>ss1
                    <div>ss2</div>
                </div>
            </div>
            <p>son2
                <span>small son</span>
            </p>
            <p>son3</p>
            <span>son4</span>
        </div>
    </div>

    <script>
        $(function(){
            // parent()
                //parent() 返回的是 最近一级的父级元素 亲爸爸
                console.log($('.son1').parent())

            // children() 亲儿子  <==>  子代选择器 ul>li
                $('.father').children('p').css('color','red')
                $('.father').children('div').css('color','red')
            //
        })
    </script>
</body>
</html>
运行结果及报错内容

img

我想要达到的结果

只想知道我写的children方法,为什么和它的功能不一样

  • 写回答

3条回答 默认 最新

  • hfhan_872914334 2022-11-30 11:44
    关注

    现在没什么问题,children只对第一层子级元素生效,你这里是因为color具有继承性质,所以显得所有后代都生效了,具体的更改打开控制台看就能看出来,或者换用其他的属性进行测试,比如border

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 12月8日
  • 已采纳回答 11月30日
  • 创建了问题 11月29日

悬赏问题

  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?