1.事件冒泡.html 910 B

1234567891011121314151617181920212223242526272829303132333435363738
  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: 400px;
  10. height: 400px;
  11. background: #00f;
  12. }
  13. #box1 {
  14. width: 200px;
  15. height: 200px;
  16. background: #f00;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <div id="box">
  22. <div id="box1"></div>
  23. </div>
  24. <script>
  25. // 事件冒泡
  26. // 事件:冒泡 目标 捕获
  27. let box = document.getElementById("box");
  28. let box1 = document.getElementById("box1");
  29. // box.onClick
  30. box.addEventListener("click",function() {
  31. console.log("1")
  32. },true)
  33. box1.addEventListener("click",function() {
  34. console.log("2")
  35. },true)
  36. </script>
  37. </body>
  38. </html>