What is event bubbling and capturing?
What is event bubbling and capturing?
答案1
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:
trickle down, bubble up 向下滴水,向上冒泡
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 as true
.
Example
<div>
<ul>
<li></li>
</ul>
</div>
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 the div
will fire first), then in the ul
, 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 the ul
, and at last by the div
element.
For more information, see
- Event Order on QuirksMode
- addEventListener on MDN
- Events Advanced on QuirksMode
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.
var logElement = document.getElementById('log');
function log(msg) {
logElement.innerHTML += ('<p>' + msg + '</p>');
}
function capture() {
log('capture: ' + this.firstChild.nodeValue.trim());
}
function bubble() {
log('bubble: ' + this.firstChild.nodeValue.trim());
}
var divs = document.getElementsByTagName('div');
for (var i = 0; i < divs.length; i++) {
divs[i].addEventListener('click', capture, true);
divs[i].addEventListener('click', bubble, false);
}
p {
line-height: 0;
}
div {
display:inline-block;
padding: 5px;
background: #fff;
border: 1px solid #aaa;
cursor: pointer;
}
div:hover {
border: 1px solid #faa;
background: #fdd;
}
<div>1
<div>2
<div>3
<div>4
<div>5</div>
</div>
</div>
</div>
</div>
<section id="log"></section>
Event Flow
3. DOM Event Architecture
This section is non-normative. Refer to [DOM] for a normative description of the DOM event architecture
3.1. Event dispatch and DOM event flow
This section gives a brief overview of the event dispatch mechanism and describes how events propagate through the DOM tree.
Applications can dispatch event objects using the dispatchEvent()
method, and the event object will propagate through the DOM tree as determined by the DOM event flow.
Event objects are dispatched to an event target. But before dispatch can begin, the event object’s propagation path must first be determined.
The propagation path is an ordered list of current event targets through which the event passes. This propagation path reflects the hierarchical tree structure of the document. The last item in the list is the event target, and the preceding items in the list are referred to as the target’s ancestors, with the immediately preceding item as the target’s parent.
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.
扩展阅读
What is event bubbling and capturing?的更多相关文章
- javascript event bubbling and capturing (再谈一谈js的事件冒泡和事件补获,看到这篇文章加深了理解)
原文地址:http://javascript.info/tutorial/bubbling-and-capturing 先给出最终的结论: Summary Events first are captu ...
- 事件冒泡(event bubbling)与事件捕捉(event capturing)
事件捕捉: 单击<div>元素就会以下列顺序触发click 事件. Document => Element html => Element body => Element ...
- js event bubble and capturing
Bubble: pppppp Capturing pppppp Mix pppppp To Stop Bubble pppppp // JS Bin
- How to correctly use preventDefault(), stopPropagation(), or return false; on events
How to correctly use preventDefault(), stopPropagation(), or return false; on events I’m sure this h ...
- 事件(event),正则
1.事件(event):事件是可以被 JavaScript 侦测到的行为.网页中的每个元素都可以产生某些可以触发 JavaScript 函数的事件.2.事件源: 触发事件的元素 事件: 被 JavaS ...
- HTML DOM Event对象
我们通常把HTML DOM Event对象叫做Event事件 事件驱动模型 事件源:(触发事件的元素)事件源对象是指event对象 其封装了与事件相关的详细信息. 当事件发生时,只能在事件函数内部访问 ...
- Javascript Event
事件原理 JS的事件原理,先看一段HTML. <html> <head> <title>Example</title> </head> &l ...
- 理解JavaScript中的事件处理 阻止冒泡event.stopPropagation();
原文地址:http://www.cnblogs.com/binyong/articles/1750263.html 这篇文章对于了解Javascript的事件处理机制非常好,将它全文转载于此,以备不时 ...
- HTML:Event [转]
这篇文章对于了解Javascript的事件处理机制非常好,将它全文转载于此,以备不时之需. 什么是事件? 事件(Event)是JavaScript应用跳动的心脏 ,也是把所有东西粘在一起的胶水.当我们 ...
随机推荐
- faces
install Boost [boost_1_65_1-msvc-14.0-32.exe]BOOST_LIBRARYDIR=D:\_softwares_kits\boost_1_65_1\lib32- ...
- Beta冲刺阶段3.0
1. 提供当天站立式会议照片一张 2. 每个人的工作 (有work item 的ID) 成员 昨天已完成的工作 今天计划完成的工作 工作中遇到的困难 具体贡献 郑晓丽 完成"我的活动&quo ...
- HTTP GET的VC三种方式
一.第一种方式(包装类的方式) a.在VC++添加类,选择typelib b.选择文件:C:\Windows\System32\winhttp.dll 选择到右边生成的类,最后点完成,即产生了一个CW ...
- 20155228 2016-2017-2 《Java程序设计》第8周学习总结
20155228 2016-2017-2 <Java程序设计>第8周学习总结 教材学习内容总结 NIO与NIO2 NIO使用频道来衔接数据节点,在处理数据时,NIO可以让你设定缓冲区容量, ...
- OS Tools-GO富集分析工具的使用与解读详细教程
我们的云平台上的GO富集分析工具,需要输入的文件表格和参数很简单,但很多同学都不明白其中的原理与结果解读,这个帖子就跟大家详细解释~ 一.GO富集介绍: Gene Ontology(简称G ...
- HTML转义符
空格的替代符号有以下几种: 名称 编号 描述 &#; 不断行的空白(1个字符宽度) &#; 半个空白(1个字符宽度) &#; 一个空白(2个字符宽度) & ...
- tensorflow学习2-线性拟合和神经网路拟合
线性拟合的思路: 线性拟合代码: import tensorflow as tf import numpy as np import matplotlib.pyplot as plt #%%图形绘制 ...
- 转:SQL Server游标的使用
使用游标步骤:1.在某个查询的基础上声明游标 --声明游标 declare c_Customers cursor for --查询所有店铺客户的客户编号 下面我们来看游标定义的参数: LOCAL和GL ...
- ESB(Enterprise Service Bus)企业服务总线介绍
ESB(Enterprise Service Bus)企业服务总线介绍 ESB全称为Enterprise Service Bus,即企业服务总线.它是传统中间件技术与XML.Web服务等技术结合的产物 ...
- 51Nod 1256 乘法逆元
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1256 给出2个数M和N(M < N),且M与N互质,找 ...