页面背景开关灯

<!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>
<body>
    <input id="btn" type="button" value="关灯">

    <script>
        var btn = document.getElementById("btn");

        btn.onclick = function(){
            if(btn.value == "关灯"){
                document.body.style.backgroundColor = "black";
                this.value = "开灯";
            }else {
                document.body.style.backgroundColor = "white";
                this.value = "关灯";
            }
            
        };
    </script>
</body>
</html>
posted @ 2021-08-13 15:03  charonmomo  阅读(35)  评论(0编辑  收藏  举报