什么是事件冒泡和捕获?
事件冒泡和捕获有什么区别?什么时候应该使用冒泡和捕获?
What is the difference between event bubbling and capturing? When should one use bubbling vs capturing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
事件冒泡和捕获是 HTML DOM API 中事件传播的两种方式,当一个事件发生在另一个元素内的一个元素中,并且两个元素都注册了该事件的句柄时。事件传播模式决定元素接收事件的顺序。
通过冒泡,事件首先由最里面的元素捕获和处理,然后传播到外部元素。
通过捕获,事件首先被最外面的元素捕获并传播到内部元素。
捕获也称为“滴流”,这有助于记住传播顺序:
,Netscape 提倡事件捕获,而 Microsoft 则提倡事件冒泡。两者都是 W3C 文档对象模型事件 标准的一部分(2000)。
IE < 9 使用仅事件冒泡,而 IE9+ 和所有主要版本浏览器都支持。另一方面,对于复杂的 DOM,事件冒泡的性能可能会稍低。
我们可以使用 addEventListener(type,listener,useCapture) 来注册冒泡(默认)或捕获模式的事件处理程序。要使用捕获模型,请将第三个参数传递为
true
。示例
在上面的结构中,假设在
li
元素中发生了单击事件。在捕获模型中,事件将首先由
div
处理(div
中的单击事件处理程序将首先触发),然后在ul
中处理,然后是目标元素的最后一个li
。在冒泡模型中,会发生相反的情况:事件将首先由
li
处理,然后由ul
处理,最后由div
处理。代码> 元素。有关详细信息,请参阅
在下面的示例中,如果单击任何突出显示的元素,您可以看到事件传播流的捕获阶段首先发生,然后是冒泡阶段。
JSFiddle 的另一个示例。
Event bubbling and capturing are two ways of event propagation in the HTML DOM API, when an event occurs in an element inside another element, and both elements have registered a handle for that event. The event propagation mode determines in which order the elements receive the event.
With bubbling, the event is first captured and handled by the innermost element and then propagated to outer elements.
With capturing, the event is first captured by the outermost element and propagated to the inner elements.
Capturing is also called "trickling", which helps remember the propagation order:
Back in the old days, Netscape advocated event capturing, while Microsoft promoted event bubbling. Both are part of the W3C Document Object Model Events standard (2000).
IE < 9 uses only event bubbling, whereas IE9+ and all major browsers support both. On the other hand, the performance of event bubbling may be slightly lower for complex DOMs.
We can use the
addEventListener(type, listener, useCapture)
to register event handlers for in either bubbling (default) or capturing mode. To use the capturing model pass the third argument astrue
.Example
In the structure above, assume that a click event occurred in the
li
element.In capturing model, the event will be handled by the
div
first (click event handlers in thediv
will fire first), then in theul
, then at the last in the target element,li
.In the bubbling model, the opposite will happen: the event will be first handled by the
li
, then by theul
, and at last by thediv
element.For more information, see
In the example below, if you click on any of the highlighted elements, you can see that the capturing phase of the event propagation flow occurs first, followed by the bubbling phase.
Another example at JSFiddle.
描述:
quirksmode.org对此有一个很好的描述。简而言之(从 quirksmode 复制):
使用什么?
这取决于您想做什么。没有更好的了。区别在于事件处理程序的执行顺序。大多数情况下,在冒泡阶段触发事件处理程序是没问题的,但也可能需要更早地触发它们。
Description:
quirksmode.org has a nice description of this. In a nutshell (copied from quirksmode):
What to use?
It depends on what you want to do. There is no better. The difference is the order of the execution of the event handlers. Most of the time it will be fine to fire event handlers in the bubbling phase but it can also be necessary to fire them earlier.
如果有两个元素,元素 1 和元素 2。元素 2 在元素 1 内部,我们为这两个元素附加一个事件处理程序,比如 onClick。现在,当我们单击元素 2 时,这两个元素的 eventHandler 将被执行。现在的问题是事件将按什么顺序执行。如果元素 1 附加的事件首先执行,则称为事件捕获;如果元素 2 附加的事件首先执行,则称为事件冒泡。
根据 W3C,事件将在捕获阶段开始,直到到达目标返回到元素,然后开始冒泡。
捕获和冒泡状态由 addEventListener 方法的 useCapture 参数得知
默认情况下 useCapture 为 false。这意味着它正处于冒泡阶段。
请尝试更改 true 和 false。
If there are two elements element 1 and element 2. Element 2 is inside element 1 and we attach an event handler with both the elements lets say onClick. Now when we click on element 2 then eventHandler for both the elements will be executed. Now here the question is in which order the event will execute. If the event attached with element 1 executes first it is called event capturing and if the event attached with element 2 executes first this is called event bubbling.
As per W3C the event will start in the capturing phase until it reaches the target comes back to the element and then it starts bubbling
The capturing and bubbling states are known by the useCapture parameter of addEventListener method
By Default useCapture is false. It means it is in the bubbling phase.
Please try with changing true and false.
我发现这个位于 javascript.info 的教程在解释这个主题时非常清楚。而最后的三点总结,真是说到了要害。我在这里引用一下:
I have found this tutorial at javascript.info to be very clear in explaining this topic. And its 3-points summary at the end is really talking to the crucial points. I quote it here:
DOM 事件描述了事件传播的 3 个阶段: 捕获阶段 – 事件深入到元素。目标阶段——事件到达目标元素。冒泡阶段——事件从元素中冒泡。
DOM Events describes 3 phases of event propagation: Capturing phase – the event goes down to the element. Target phase – the event reached the target element. Bubbling phase – the event bubbles up from the element.
还有
Event.eventPhase
属性可以告诉您事件是在目标处还是来自其他地方,并且浏览器完全支持它。扩展已经来自已接受答案的精彩片段,这是使用
eventPhase
属性的输出There's also the
Event.eventPhase
property which can tell you if the event is at target or comes from somewhere else, and it is fully supported by browsers.Expanding on the already great snippet from the accepted answer, this is the output using the
eventPhase
property冒泡
捕获
Bubbling
Capturing
当浏览器检测到事件时,它会尝试查找事件处理程序。此过程分为 3 个阶段。假设我们有这些元素
1-Capture Phase
浏览器将查看刚刚单击的元素。然后它将转到顶部父元素,即
body
。如果body中有任何点击句柄,浏览器就会调用它。检查 body 元素后,它将查看第二个顶部父元素,即div
元素。这个过程将重复,直到浏览器到达最底部的按钮元素。一旦它看到按钮元素,第一阶段-捕获就会结束。大多数时候我们都会忽略这个阶段。这段代码忽略了这个阶段,如果想涉及到这个阶段,就得这样写,
当我们需要检测目标之外的点击时,就需要这个阶段。也许我们打开了一个下拉菜单或模式,并且我们想要关闭它,如果用户单击模式或下拉菜单
2-Target Phase
之外的任何位置,浏览器将查看单击的元素,即
Button
和如果按钮有事件处理程序,它将调用它。3- 冒泡阶段
与
Capture Phase
相反,在此阶段,浏览器将从直接父元素(在本例中为div
元素)开始该过程,然后它将访问body
元素。此代码将为冒泡阶段设置事件处理程序When browser detects an event, it tries to find an event handler. This process has 3 phases. Let's say we have these elements
1-Capture Phase
browser is going to take a look at the element that was just clicked. Then it will go to the top parent element which is
body
. if there is any click handle in body, the browser will call it. after checking the body element, it will look at the second top parent element which isdiv
element. This process will repeat until the browser gets down to the button element at the very bottom. Once it sees the button element, Phase One- Capturing will be over. Most of the time we ignore this phase. This code ignores this phaseIf you want to involve this phase, you have to write this
We need this phase when we need to detect a click outside the target. maybe we have a dropdown or a modal open and we want to close it if the user clicks on anywhere outside the modal or dropdown
2-Target Phase
the browser will look at the clicked element which is the
Button
and if the button has an event handler, it will call it.3- Bubble Phase
Opposite to
Capture Phase
, in this phase, browser will start the process from the immediate parent element which isdiv
element in this case and then it will visit thebody
element. This code will set up the event handler for the bubble phase我做了一个小例子,您可以在其中体验“事件冒泡”: https:// /codepen.io/abernier/pen/yKGJXK?editors=1010
单击任意 div,您将看到“click”事件冒泡!
I made a small example, where you can experience "event-bubbling" live: https://codepen.io/abernier/pen/yKGJXK?editors=1010
Click any div, and you'll see the "click" event bubbling up!
正如其他人所说,冒泡和捕获描述了某些嵌套元素接收给定事件的顺序。
我想指出的是,对于最里面元素可能会出现一些奇怪的情况。事实上,对于某些浏览器(例如 Mozilla Firefox),事件侦听器的添加顺序确实很重要。
在下面的例子中,
div2
的捕获将先于冒泡执行;而div4
的冒泡将先于捕获执行。备注:尝试使用 Mozilla Firefox 运行上面的代码片段。
编辑:到 2024 年,这个问题似乎已经得到解决。
As other said, bubbling and capturing describe in which order some nested elements receive a given event.
I wanted to point out that for the innermost element may appear something strange. Indeed, for some browser (e.g. on Mozilla Firefox), the order in which the event listeners are added does matter.
In the following example, capturing for
div2
will be executed first than bubbling; while bubbling fordiv4
will be executed first than capturing.Remark: try running the snippet above with Mozilla Firefox.
EDIT: By 2024 this issue seems to have been fixed.
这些是javascript中的事件传播过程。
事件冒泡:
在这种情况下,事件首先被捕获,并由事件发生的最里面的元素处理,然后它通过 DOM 层次结构中的父元素传播。
事件捕获:
在这种情况下,事件首先由最外面的元素捕获并处理,然后向下传播。
These are event propagation process in javascript.
Event Bubbling:
In this, the event is first captured and it is handled by the innermost element, where the event occurred, then it propagates through parent elements in the DOM hierarchy.
Event Capturing:
In this case, the event is first captured and handled by the outermost element, then it propagates downward.