爱编程的设计师 2022-09-24 20:07 采纳率: 94.4%
浏览 28
已结题

为什么动态创建的元素,删除按钮的背景色打印不出来

问题遇到的现象和发生背景
用代码块功能插入代码,请勿粘贴截图
我想要达到的结果
<!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>Document</title>
</head>
    <style>
        *{
            margin: 0px;
            padding: 0px;
        }
        a,ol{
            list-style: none;
        }
        .home{
            margin: 100px auto;
            width: 350px;
            background-color: antiquewhite;
        }
        .btn{
            height: 35px;
            width: 150px;
        }
        textarea{
            width: 300px;
            height: 200px;
        }
        li:nth-child(even){
            background-color: blueviolet;
        }
        .home>div:nth-child(2){
            background-color: aquamarine;
        }
        .colorBox{
            box-sizing:border-box;
            padding-left: 10px;
            position: relative;
            width: 100%;
            height: 30px;
            font-size: 15px;
            line-height: 30px;
            background: linear-gradient(#DC1010, #ed9a5a, #dce82f);
        }
        .del{
            position: absolute;
            right: 20px;
            width: 60px;
            height: 18px;
            top: 50%;
            margin-top:-9px ;
            background-color: rgb(47, 93, 244);
            border: 0px solid black;
            border-radius: 5px;
            outline: none;
        }
    </style>
<body>
    <div class="home">
        <div>
            <button class="btn">press</button><br>
            <textarea placeholder="请输入名字"></textarea>
        </div>
        <div>
            <ol>
                <li>good morning</li>
                <li>good morning</li>
                <li>good morning</li>
                <li>good morning</li>
                <li>good morning</li>
            </ol>
        </div>
    </div>

    <script>
        let btn = document.querySelector('.btn')
        btn.addEventListener('click',function(){
            let color = document.createElement('div')
            color.className = 'colorBox'
            color.innerHTML=`
            <input type="checkBox">
            <span>男/女</span>
            <button class="del">删除</button>`
            document.querySelector('.home').appendChild(color)
            color.onclick = function(e){
                if(e.target.className=='del'){
                    console.dir(e.target.style);
                    window.setTimeout(()=>{
                        this.parentNode.removeChild(this)
                    },1000)
                }else{
                    console.log('no');
                }
                
            }
        })
    </script>
</body>

</html>
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/743962120466175.jpg "#left")


img

  • 写回答

1条回答 默认 最新

  • MAXLZ 2022-09-24 21:27
    关注

    style属性只包含使用标签中使用style指定的样式,不包含外部样式。

    如果你想获取外部样式,你可以使用window.getComputedStyleElement.computedStyleMap()来获取元素所有样式。

    另外getComputedStyle兼容性比computedStyleMap兼容性好很多。

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

报告相同问题?

问题事件

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

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装