2.demo.html 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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: 200px;
  10. height: 200px;
  11. background: #f00;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div class="box"></div>
  17. <button id="btn1">显示</button>
  18. <button id="btn2">隐藏</button>
  19. <script>
  20. // 需求:改变盒子的大小及颜色
  21. // 显示在浏览器五秒后改变
  22. var box = document.querySelector(".box");
  23. var btn1 = document.getElementById("btn1");
  24. var btn2 = document.getElementById("btn2");
  25. setTimeout(()=>{
  26. console.log(box);
  27. box.style.width = "300px";
  28. box.style.height = "300px";
  29. box.style.background = "#00f";
  30. },5000)
  31. btn1.onclick = function() {
  32. box.style.display = "block";
  33. }
  34. btn2.onclick = function() {
  35. box.style.display = "none";
  36. }
  37. </script>
  38. </body>
  39. </html>