为什么点击关闭时body的backgroundColor颜色不能变为白色了

<!DOCTYPE html>
<html>
<head>
    <title>浮动窗口练习</title>
    <meta charset="utf-8">
    <style type="text/css">
        body {
            padding: 0;
            margin: 0;
            /* background-color: gray; */
        }
        .cover {
            position: fixed;
            top: 0;
            left: 0;
            display: none;
            background-color: rgba(255,255,255,0.2);
            /* opacity: 0.2; */
        }
        .alert1 {
            width: 400px;
            height: 200px;
            border: 2px solid blue;
            border-radius: 5px;
            background-color: cyan;
            margin-top: 100px;
            margin-left: 500px;
            display: inline-block;
        }
    </style>
</head>
<body>
        <input type="button" value="点我看看" onclick="a.alertWindow()">
        <a href=""></a>
</body>
</html>
<script type="text/javascript">
    // function click0 () { 
    //     alertWindow();
    // }
    function A (name) {
        this.name = name;
        
      this.alertWindow = function () {
          document.body.style.backgroundColor = "rgb(255,255,255)";
        var div = document.createElement('div');
        div.setAttribute("class", "cover");
        var div0 ="<div class = 'alert1'>";
        div0 += '<div class="content"></div>';
        div0 += '<div class="tool">';
        div0 += '<p>' + name + '</p>';
        div0 += '<input type="button" value="确定" id="Ok">';
        div0 += '<input type="button" value="关闭" id="Cancle">';
        div0 += '</div>';
        div0 += '</div>';
        div.innerHTML = div0;
        document.body.appendChild(div);
        div.style.display = 'block';

        var cover = document.getElementsByClassName('cover')[0];
        this.cover = div;
        // var that = this;
        var Ok1 = document.getElementById('Ok');
        var Cancle1 = document.getElementById('Cancle');
        var that = this;
        Ok1.onclick = function  () {
            // cover.style.display = "none";
            var x = 255;
            var y = 0;
            function change () {
                x--;
                y++;        
                document.body.style.backgroundColor = "rgb(255," + y + "," + x + ")";
                setInterval(change, 200);
                if (x == 0) {
                    clearInterval(change);
                    // document.body.style.backgroundColor = "rgb(255," + y + "," + x + ")";
                }    
            }
            setTimeout(change, 200);
            that.close();
        }
        Cancle1.onclick = function () {    
            // cover.style.display = "none";
            document.body.style.backgroundColor = "rgb(0,255,0)";
            that.close();
            // var bgColor = document.getElementsByTagName('body');
            // bgColor.style.backgroundColor = "white";
            
        }
         this.adjust();
    }
    this.close = function () {
        // cover.remove();
        this.cover.remove();
    }
    this.adjust = function () {

        // 1 让弹出层的大小跟窗口一样大
        this.cover.style.width = window.innerWidth+"px";
        this.cover.style.height = window.innerHeight+"px";
    }
    }    
    var a = new A("百度");
    

</script>
</body>
阅读 3.6k
3 个回答
Cancle1.onclick = function () {    
            // cover.style.display = "none";
            document.body.style.backgroundColor = "rgb(0,255,0)";
            that.close();
            // var bgColor = document.getElementsByTagName('body');
            // bgColor.style.backgroundColor = "white";
            
        }

你设置的backgroundColor不是绿色的吗?

孩子,写错了,你设置的是绿的。试试把取消方法的代码换成这个。
document.body.style.backgroundColor = "rgb(255,255,255)";

另外你这个命名很有问题,Cancel是取消,你拼错了。

rgb(255,255,255)

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题