10.小例子.html 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. * {
  9. margin: 0;
  10. padding: 0;
  11. box-sizing: border-box;
  12. }
  13. .box {
  14. width: 200px;
  15. height: 200px;
  16. background: #00f;
  17. }
  18. button {
  19. margin-top: 40px;
  20. }
  21. .vase {
  22. width: 50px;
  23. height: 35px;
  24. text-align: center;
  25. line-height: 35px;
  26. background: purple;
  27. color: #fff;
  28. margin-top: 40px;
  29. margin-left: 20px;
  30. }
  31. </style>
  32. </head>
  33. <body>
  34. <div class="box"></div>
  35. <button>改变颜色</button>
  36. <ul>
  37. <li>12</li>
  38. <li>34</li>
  39. <li>56</li>
  40. <li>78</li>
  41. </ul>
  42. <div class="vase">改变</div>
  43. <script>
  44. var btn = document.querySelector("button");
  45. var box = document.querySelector(".box");
  46. var lis = document.querySelectorAll("ul li");
  47. var vase = document.querySelector(".vase");
  48. btn.onclick = function() {
  49. box.style.marginLeft = 100 + 'px';
  50. box.style.background = 'red';
  51. }
  52. vase.onclick = function() {
  53. lis[0].style.background = 'red';
  54. }
  55. </script>
  56. </body>
  57. </html>