1.事件冒泡.html 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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: 500px;
  10. height: 500px;
  11. background-color: #f00;
  12. }
  13. .box1 {
  14. width: 300px;
  15. height: 300px;
  16. background-color: #ff0;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <div class="box">
  22. <div class="box1"></div>
  23. </div>
  24. <script>
  25. // 事件三节段:事件捕获 目标阶段 事件冒泡
  26. const box = document.querySelector(".box");
  27. const box1 = document.querySelector(".box1");
  28. // box.onclick = function () {
  29. // console.log("box");
  30. // }
  31. // box1.onclick = function () {
  32. // console.log("box1");
  33. // }
  34. //true 事件捕获阶段
  35. // false 事件冒泡阶段
  36. box.addEventListener('click', function () {
  37. console.log("box");
  38. }, false)
  39. box1.addEventListener('click', function () {
  40. console.log("box1");
  41. }, false)
  42. </script>
  43. </body>
  44. </html>