6_DOM_控制样式.html 1003 B

1234567891011121314151617181920212223242526272829303132
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. .box{
  9. width: 100px;
  10. height: 100px;
  11. background-color: red;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <button>按钮</button>
  17. <div class="box"></div>
  18. <script>
  19. var oBox = document.getElementsByClassName("box");
  20. var oBtn = document.getElementsByTagName("button");
  21. // style 控制元素的样式 style 后边跟着的是具体的样式名称
  22. // 样式名称如果是复合命名(多个单词构成 background-color) 则需要将中间的-去掉 并将后边的首字母大写(backgroundColor)
  23. oBtn[0].onclick = function(){
  24. var num = 200;
  25. oBox[0].style.width = num + "px";
  26. oBox[0].style.height = "200px";
  27. oBox[0].style.backgroundColor = "green";
  28. }
  29. </script>
  30. </body>
  31. </html>