| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- <style>
- .box{
- width: 200px;
- height: 200px;
- background-color: red;
- }
- .active{
- background-color: blue;
- }
- .div1{
- font-size: 50px;
- }
- </style>
- </head>
- <body>
- <div class="box div1">hello</div>
- <button id="btn">切换类名</button>
- <script>
- // 获取元素
- var box = document.querySelector(".box");
- var btn = document.querySelector("#btn");
- // 绑定事件
- btn.onclick = function(){
- // 操作类 classList 方法
- // add()方法可以添加类名 只是在基础上添加类名 不会覆盖之前的类名
- // box.classList.add("active");
- // remove()
- // box.classList.remove("div1");
- }
- </script>
- </body>
- </html>
|