博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
对javascript事件的深度理解
阅读量:7235 次
发布时间:2019-06-29

本文共 2101 字,大约阅读时间需要 7 分钟。

事件流

事件流是描述对页面接受事件的顺序,IE和Netscape提出了完全相反的事件流模型,描述的是从页面中接收事件的顺序,也可理解为事件在页面中传播的顺序。

我们通过平常使用知道addEventListener最后的参数是切换句柄的,当这个布尔值为true时,表示在捕获阶段调用事件处理程序;若果是false,表示在冒泡阶段调用事件处理程序。

MDN:useCapture 可选()

Boolean,是指在DOM树中,注册了该listener的元素,是否会先于它下方的任何事件目标,接收到该事件。沿着DOM树向上冒泡的事件不会触发被指定为use capture(也就是设为true)的listener。当一个元素嵌套了另一个元素,两个元素都对同一个事件注册了一个处理函数时,所发生的事件冒泡和事件捕获是两种不同的事件传播方式。事件传播模式决定了元素以哪个顺序接收事件。

Once the propagation path has been determined, the event object passes through one or more event phases. There are three event phases: capture phase, target phase and bubble phase. Event objects complete these phases as described below. A phase will be skipped if it is not supported, or if the event object’s propagation has been stopped. For example, if the bubbles attribute is set to false, the bubble phase will be skipped, and if stopPropagation() has been called prior to the dispatch, all phases will be skipped.

The capture phase: The event object propagates through the target’s ancestors from the Window to the target’s parent. This phase is also known as the capturing phase.

The target phase: The event object arrives at the event object’s event target. This phase is also known as the at-target phase. If the event type indicates that the event doesn’t bubble, then the event object will halt after completion of this phase.

The bubble phase: The event object propagates through the target’s ancestors in reverse order, starting with the target’s parent and ending with the Window. This phase is also known as the bubbling phase.

为什么要使用 addEventListener?

addEventListener 是 W3C DOM 规范中提供的注册事件监听器的方法。它的优点包括:

  1. 它允许给一个事件注册多个 listener。当存在其他的库时,使用 DHTML 库或者 Mozilla extensions 不会出现问题。

  2. 它提供了一种更精细的手段控制 listener 的触发阶段。(即可以选择捕获或者冒泡)。

  3. 它对任何 DOM 元素都是有效的,而不仅仅只对 HTML 元素有效。

事件冒泡

IE的事件流叫事件冒泡,逐级向上传播

            

点击div之后,顺序是div -> body -> html

事件捕获

Netscape事件捕获是与冒泡相反的

DOM事件流

DOM规定的事件流包括三个阶段

  1. 捕获阶段

  2. 目标阶段

  3. 冒泡阶段

事件捕获阶段,为截获事件提供机会,然后是实际的目标接受事件,最后是事件冒泡阶段

  
Document

事件的作用范围

事件的作用范围为:

元素自己所占页面空间部分加嵌套元素所占空间范围(若嵌套元素覆盖在容器元素上,则事件的作用范围为容器元素自身所占空间大小)

转载地址:http://uagfm.baihongyu.com/

你可能感兴趣的文章
「镁客·请讲」因诺航空胡军:无人机行业进入洗牌期,我们渴望完整的产业链...
查看>>
区块链钱包开发
查看>>
游戏公司盯上了区块链:是机会,还只是一场游戏?
查看>>
工控主板定制ARM9选择思路
查看>>
PHP实现远程下载文件到本地
查看>>
Matplotlib 中文用户指南 7.3 事件处理及拾取
查看>>
IBM新思路,让无人机照看、训练你的宝贝萌宠
查看>>
使用 AppImageLauncher 轻松运行和集成 AppImage 文件
查看>>
区块链技术指北社区(Chain One Community)规划
查看>>
善用金融科技——第十四届中国区域商业银行信息化发展战略高峰年会5月开幕...
查看>>
镁客网首场香港活动,与海内外人士共话AI革新
查看>>
操作ACCESS数据库注意事项
查看>>
禁止IE7的页面缩放功能
查看>>
java多线程的等待唤醒机制及如何解决同步过程中的安全问题
查看>>
捷径 - The certain shortcut
查看>>
IE 的浏览器模式和文本模式(二)
查看>>
css3 背景
查看>>
LVS的DR和NAT模式配置
查看>>
总结概括对于大数据、高并发的网站如何进行优化的问题
查看>>
学习javascript必须订阅30个程序员的Blog
查看>>