JS给DIV添加CSS样式及显示与隐藏

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <meta charset="utf-8" />
    <style type="text/css">
        #dv1 {
        }

        .testClass1 {
            height: 450px;
            width: 450px;
            border: 1px solid blue;
        }

        .testClass2 {
            background-color: aqua;
        }
    </style>
    <script type="text/javascript">
        window.onload = function () {
            var isbool = true;
            //给DIV添加CSS class 样式
            document.getElementById('btn1').onclick = function () {
                if (document.getElementById('dv1').className == '') {
                    document.getElementById('dv1').className = 'testClass1';
                } else {
                    document.getElementById('dv1').className += ' testClass1';
                }
            }
            //给DIV添加CSS class 样式
            document.getElementById('btn2').onclick = function () {
                if (document.getElementById('dv1').className == '') {
                    document.getElementById('dv1').className = 'testClass2';
                } else {
                    document.getElementById('dv1').className += ' testClass2';
                }
            }
            //控制显示隐藏
            document.getElementById('btn3').onclick = function () {
                if (isbool == true) {
                    document.getElementById('dv1').style.display = 'none';
                   this.value = "显示";
                    isbool = false;
                } else {
                    document.getElementById('dv1').style.display = '';
                    isbool = true;
                    this.value = "隐藏";
                }
            }
        }
    </script>
</head>
<body>
    <input type="button" value="button" id="btn1" />
    <input type="button" value="button2" id="btn2" />
    <input type="button" value="隐藏" id="btn3" />
    <div id="dv1">11</div>
</body>
</html>

评论