JavaScript(DOM)事件流--事件捕获、事件冒泡、阻止事件冒泡

阳光穿透心脏的1/2处 2022-12-23 15:23 438阅读 0赞

DOM事件流

事件流描述的是从页面中接收事件的顺序。

事件发生时会在元素节点之间按照特定的顺序传播,这个传播过程即DOM事件流。

DOM事件流分为3个阶段: 捕获阶段 当前目标段 冒泡阶段

JS代码中只能执行捕获或者冒泡其中一个阶段。onclick和attachEvent只能得到冒泡阶段

因此,在addEventListener()第三个参数如果是true,则表示捕获阶段调用时间处理程序。吐过第三个参数是false或者为空,则表示在事件冒泡阶段调用事件处理程序。

  1. <head>
  2. <style>
  3. .father{
  4. width: 500px;
  5. height: 500px;
  6. margin: auto;
  7. background-color: blue;
  8. }
  9. .son{
  10. width: 200px;
  11. height: 200px;
  12. margin: auto;
  13. background-color: pink;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <div class="father">
  19. <div class="son">son盒子 </div>
  20. </div>
  21. <script>
  22. var son = document.querySelector('.son');
  23. var father = document.querySelector('.father');
  24. //捕获阶段 document -> html -> body -> father -> son
  25. // son.addEventListener('click',function(){
  26. // alert('son');
  27. // },true);
  28. // father.addEventListener('click',function(){
  29. // alert('father');
  30. // },true);
  31. //点击son盒子,会先弹出father提示框,然后再弹出son提示框
  32. //捕获阶段和冒泡阶段只能进行一个,所以注释了捕获阶段
  33. //冒泡阶段 son -> father -> body -> html -> document
  34. son.addEventListener('click',function(){
  35. alert('son');
  36. },false);
  37. father.addEventListener('click',function(){
  38. alert('father');
  39. },false);
  40. //点击son盒子,会先弹出son提示框,然后再弹出father提示框
  41. </script>
  42. </body>

捕获阶段程序运行,点击son,先弹出father再弹出son

在这里插入图片描述
在这里插入图片描述

冒泡阶段,先弹出son,再弹出father

在这里插入图片描述
在这里插入图片描述
有些事件是没有冒泡的,例如:onblur,onfocus,onmouseenter,onmouseleave

阻止事件冒泡

标准写法:利用事件对象里面的stopPropagation()方法。

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>事件流</title>
  7. <style>
  8. .father{
  9. width: 500px;
  10. height: 500px;
  11. margin: auto;
  12. background-color: blue;
  13. }
  14. .son{
  15. width: 200px;
  16. height: 200px;
  17. margin: auto;
  18. background-color: pink;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div class="father">
  24. <div class="son">son盒子 </div>
  25. </div>
  26. <script>
  27. var son = document.querySelector('.son');
  28. var father = document.querySelector('.father');
  29. //冒泡阶段 son -> father -> body -> html -> document
  30. son.addEventListener('click',function(e){
  31. alert('son');
  32. e.stopPropagation();//阻止冒泡
  33. //e.cancelBubble = true;阻止冒泡,IE6/7/8使用的方法。
  34. },false);
  35. father.addEventListener('click',function(){
  36. alert('father');
  37. },false);
  38. document.addEventListener('click',function(){
  39. alert('document');
  40. },false)
  41. //点击son盒子,只弹出son提示框
  42. </script>
  43. </body>
  44. </html>

通过方法stopPropagation()阻止冒泡。
非准写法:利用事件对象cancelBubble属性。
上述程序在son后阻止冒泡,但是如果点击father盒子,则会弹出father提示框,点击确定,接着会弹出document对话框。并没有阻止后面的冒泡。

发表评论

表情:
评论列表 (有 0 条评论,438人围观)

还没有评论,来说两句吧...

相关阅读

    相关 阻止事件冒泡

    事件的冒泡(Bubble) \- 所谓的冒泡指的就是事件的向上传导,当后代元素上的事件被触发时,其祖先元素的相同事件也会被触发 \- 在开发中大部分情况冒泡都是有用的,如果

    相关 JS 事件 事件冒泡捕获

    JavaScript与HTML之间的交互是通过事件实现的。事件,就是文档或浏览器窗口中发生的一些特定的交互瞬间。可以使用侦听器来预订事件,以便事件发生时执行相应的代码。