| 1234567891011121314151617181920212223242526272829303132 |
- <!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: 100px;
- height: 100px;
- background-color: red;
- }
- </style>
- </head>
- <body>
- <button>按钮</button>
- <div class="box"></div>
- <script>
- var oBox = document.getElementsByClassName("box");
- var oBtn = document.getElementsByTagName("button");
- // style 控制元素的样式 style 后边跟着的是具体的样式名称
- // 样式名称如果是复合命名(多个单词构成 background-color) 则需要将中间的-去掉 并将后边的首字母大写(backgroundColor)
- oBtn[0].onclick = function(){
- var num = 200;
- oBox[0].style.width = num + "px";
- oBox[0].style.height = "200px";
- oBox[0].style.backgroundColor = "green";
- }
- </script>
- </body>
- </html>
|