爱编程的设计师 2022-05-09 19:05 采纳率: 94.4%
浏览 27
已结题

javascript能改变body的背景颜色怎么不能改变父元素的相邻元素的背景颜色

问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
<!DOCTYPE=html>
    <html lang="zh-cn">

    <head>
        <meta charset="UTF-8">
        <title>
            关于文档元素测试
        </title>
        <style>

            .son1 {

                height: 30px;
                width: 100px;
                background-color: orange;
            }

            .son2 {

                height: 30px;
                width: 100px;
                background-color: red;
            }

            .son3 {
                height: 30px;
                width: 100px;
                background-color: #08a9b5;
            }


            .son4 {
                height: 30px;
                width: 100px;
                background-color: #9ad1c3;
            }

            .son5 {

                height: 30px;
                width: 100px;
                background-color: rgb(21, 123, 126);
            }
            .grand_father{
                display: inline-block;
            }
            .mother{
                box-sizing: border-box;
                width: 100%;
                height: 250px;
                margin-top: 5px;
                border: 1px solid black;
                background-color: orange;
            }
            body{
                display: flex;
                justify-content: center;
                align-items: center;
            }
        </style>
    </head>

    <body>
        <div class="grand_father">
            <div class="father">
                <button class="son1">q</button>
                <button class="son2">w</button>
                <button class="son3">e</button>
                <button class="son4">e</button>
                <button class="son5">e</button>
            </div>
            <div class="mother">
            </div>
        </div>
        <script>
            var father = document.querySelector('.father');
            var sons=father.querySelectorAll('button');
            // console.log(sons);
            for(var i=0;i<sons.length;i++ ){
                sons[i].my_index=i;
                sons[i].onclick=function(){
                    var ind=this.my_index;
                    var son_obj=window.getComputedStyle(sons[ind]);
                    var son_obj_color=son_obj.backgroundColor;
                    // document.body.style.backgroundColor= son_obj_color;
                    var mother = document.querySelector('.mother');
                    var mother_obj = window.getComputedStyle(mother);
                    var mother_obj_color = mother_obj.backgroundColor;
                    mother_obj.backgroundColor=son_obj_color;
                    // console.log(mother_obj_color);

                }
            }
        </script>
    </body>

    </html>

运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
  • 写回答

1条回答 默认 最新

  • ✎﹏ℳ๓敬坤 2022-05-09 19:13
    关注

    你是想实现说,点击上面的按钮,然后下面的盒子颜色跟按钮一样的颜色吗

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 5月18日
  • 已采纳回答 5月10日
  • 修改了问题 5月9日
  • 创建了问题 5月9日

悬赏问题

  • ¥15 安装TIA PortalV15.1报错
  • ¥15 能把水桶搬到饮水机的机械设计
  • ¥15 Android Studio中如何把H5逻辑放在Assets 文件夹中以实现将h5代码打包为apk
  • ¥15 使用小程序wx.createWebAudioContext()开发节拍器
  • ¥15 关于#爬虫#的问题:请问HMDB代谢物爬虫的那个工具可以提供一下吗
  • ¥15 vue3+electron打包获取本地视频属性,文件夹里面有ffprobe.exe 文件还会报错这是什么原因呢?
  • ¥20 用51单片机控制急停。
  • ¥15 孟德尔随机化结果不一致
  • ¥15 在使用pyecharts时出现问题
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路