事件处理的三个阶段
今天面试被问到这个问题,我听得一脸懵,啥三个阶段,我只知道触发事件,事件冒泡,别的还真不知道,上网搜索了一下,W3C的原文链接
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.
简单来说事件处理分为三个阶段:
- 事件捕获阶段
- 事件响应阶段
- 事件冒泡阶段
事件发生时:
- 第一步由window发出事件对象通过目标元素的祖先节点传播
- 第二步在事件对象到达目标元素以后,事件触发
- 第三步在事件触发以后事件对象按与事件捕获阶段相反的方向传播(子->父->window)
PS:我发现我好像隐约有点印象,但是我给忘了