事件处理模型--冒泡、捕获

本文深入探讨了事件处理中的冒泡和捕获机制,通过实例解析了这两种机制的工作原理,包括如何在不同层级的元素间传递事件,以及如何使用W3C标准和IE特有的方法来取消冒泡。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

事件处理模型–冒泡、捕获

事件冒泡

1.当一个元素接收到事件的时候 会把他接收到的事件传给自己的父级,一直到window(自底向上)
demo1

在这里插入图片描述
后面的demo可以结合这个图更好理解
html
html代码:

<div class="grandfather">
    <div class="father">
      <div class="child"></div>
  </div>
</div> 

css代码:

.grandfather{
      width: 300px;
      height: 300px;
      background-color: red;
    }
    .father{
      width: 200px;
      height: 200px;
      background-color: blue;
    }
    .child{
      width: 100px;
      height: 100px;
      background-color: yellow;
    }

js代码:

var oGrandfather = document.getElementsByClassName("grandfather")[0];
var oFather = document.getElementsByClassName("father")[0];
var oChild = document.getElementsByClassName("child")[0];

oGrandfather.addEventListener('click',function () {
  console.log("grandfather--red")
}, false)
oFather.addEventListener('click',function () {
  console.log("father--blue")
}, false)
oChild.addEventListener('click',function () {
  console.log("child--yellow")
}, false)

在这里插入图片描述
只点击黄色区域,会依次触发child–yellow,father–blue,grandfather–red

2.结构上(非视觉上)嵌套关系的元素,会存在事件冒泡的功能(从代码的角度是自底向上一层层冒泡的)
demo2

在上面css基础上添加

    .father{
      margin-left: 300px;
    }
    .child{
      margin-left: 200px;
    }

在这里插入图片描述
点击黄色区域,还是只点击黄色区域,会依次触发child–yellow,father–blue,grandfather–red触发child–yellow,father–blue,grandfather–red。说明冒泡是基于代码结构的,而不是视觉上

事件捕获

1.结构上(非视觉上)嵌套关系的元素,会存在事件捕获的功能,从父元素(从代码的角度是自底向上一层层冒泡的)
demo1

将js代码中的addeventListener中的false改为true,从而实现事件捕获

oGrandfather.addEventListener('click',function () {
  console.log("grandfather--red")
}, true)
oFather.addEventListener('click',function () {
  console.log("father--blue")
}, true)
oChild.addEventListener('click',function () {
  console.log("child--yellow")
}, true)

在这里插入图片描述
只点击黄色区域,会依次触发grandfather–red,father–blue,child–yellow,从外向内依次捕获并执行,最里面的(事件源)是按常规执行

demo2

在这里插入图片描述
也是只点击黄色区域,会从外向内依次触发grandfather–red,father–blue,child–yellow

先捕获后冒泡

同一对象的同一事件类型,上面绑定了两个事件处理函数,一个符合冒泡,一个符合捕获,点击一个元素后,先触发捕获再触发冒泡

demo

html,css代码沿用上面,变更部分js

oGrandfather.addEventListener('click',function () {
    console.log("grandfather--red--捕获事件")
}, true)
oFather.addEventListener('click',function () {
    console.log("father--blue--捕获事件")
}, true)
oChild.addEventListener('click',function () {
    console.log("child--yellow--捕获事件")
}, true)

oGrandfather.addEventListener('click',function () {
    console.log("grandfather--red--冒泡事件")
}, false)
oFather.addEventListener('click',function () {
    console.log("father--blue--冒泡事件")
}, false)
oChild.addEventListener('click',function () {
    console.log("child--yellow--冒泡事件")
}, false)

在这里插入图片描述

改变捕获和冒泡事件顺序

oGrandfather.addEventListener('click',function () {
    console.log("grandfather--red--冒泡事件")
}, false)
oFather.addEventListener('click',function () {
    console.log("father--blue--冒泡事件")
}, false)
oChild.addEventListener('click',function () {
    console.log("child--yellow--冒泡事件")
}, false)

oGrandfather.addEventListener('click',function () {
    console.log("grandfather--red--捕获事件")
}, true)
oFather.addEventListener('click',function () {
    console.log("father--blue--捕获事件")
}, true)
oChild.addEventListener('click',function () {
    console.log("child--yellow--捕获事件")
}, true)

在这里插入图片描述
效果图都为点击黄色区域触发
可见,事件触发顺序为先捕获后冒泡事件源则是按照事件绑定先后触发

取消冒泡

1.W3C标准event.stopPropagation();
2.IE独有event.cancelBubble = ture;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值