网页开关灯

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <style type="text/css">
            .cls {
                background-color: black;
            }
        </style>
    </head>
    <body class="cls">
        <input type="button" value="开/关灯" id="btn">
        <script type="text/javascript">
            function my$(id) {
                return document.getElementById(id);
            }
            my$("btn").onclick=function(){
                //获取body标签
                document.body.className=document.body.className!="cls"?"cls":"";
            }
        </script>
    </body>
</html>

猜你喜欢

转载自www.cnblogs.com/zhangDY/p/11424203.html