18.事件流.html 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. #box1 {
  9. width: 500px;
  10. height: 500px;
  11. background: red;
  12. }
  13. #box2 {
  14. width: 300px;
  15. height: 300px;
  16. background: yellowgreen;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <div id="box1">
  22. <div id="box2"></div>
  23. </div>
  24. <script>
  25. var box1 = document.getElementById("box1");
  26. // box1.onclick
  27. /**
  28. * 事件:
  29. * 捕获:true
  30. * 目标
  31. * 冒泡:false
  32. */
  33. /**
  34. * 事件流:
  35. * xxx.addEventListener("监听方法",执行函数,true/false);
  36. */
  37. box1.addEventListener("click",function(){
  38. console.log("111")
  39. },true)
  40. box2.addEventListener("click",function(){
  41. console.log("222")
  42. // event.stopPropagation()
  43. },true)
  44. </script>
  45. </body>
  46. </html>