22.事件源.html 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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: 300px;
  10. height: 300px;
  11. background: #f00;
  12. margin-top: 30px;
  13. }
  14. #box2 {
  15. width: 150px;
  16. height: 150px;
  17. background: #ff0;
  18. }
  19. #box3 {
  20. width: 300px;
  21. height: 300px;
  22. background: #00f;
  23. margin-top: 30px;
  24. }
  25. #box4 {
  26. width: 150px;
  27. height: 150px;
  28. background: #0f0;
  29. }
  30. </style>
  31. </head>
  32. <body>
  33. <!--
  34. 万维网联盟(W3C)
  35. DOM事件流:三个阶段
  36. 事件捕获
  37. 目标阶段
  38. 事件冒泡
  39. -->
  40. <div id="box1">
  41. <div id="box2"></div>
  42. </div>
  43. <div id="box3">
  44. <div id="box4"></div>
  45. </div>
  46. <script>
  47. var box1 = document.getElementById("box1");
  48. var box2 = document.getElementById("box2");
  49. var box3 = document.getElementById("box3");
  50. var box4 = document.getElementById("box4");
  51. box1.addEventListener("click",function(){
  52. console.log(this,'box1')
  53. },true)
  54. box2.addEventListener("click",function(){
  55. console.log(this,'box2')
  56. },true)
  57. box3.addEventListener("click",function(){
  58. console.log(this,'box3')
  59. },false)
  60. box4.addEventListener("click",function(){
  61. console.log(this,'box4')
  62. },false)
  63. // removeEventListener('监听的时间方法',执行的函数,触发的事件类型:true (事件捕获) / false(事件冒泡))
  64. </script>
  65. </body>
  66. </html>