博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
What is event bubbling and capturing?
阅读量:4347 次
发布时间:2019-06-07

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

答案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 .

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 standard (2000).

IE < 9 uses , whereas IE9+ and all major browsers support both. On the other hand, the 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

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

  • on QuirksMode
  • on MDN
  • 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 += ('

' + msg + '

'); } 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; }
1
2
3
4
5
 

 

.

 

3. DOM Event Architecture

This section is non-normative. Refer to  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  mechanism and describes how events propagate through the DOM tree.

Applications can dispatch event objects using the  method, and the event object will propagate through the DOM tree as determined by the DOM event flow.

 

 

Event objects are dispatched to an . But before dispatch can begin, the event object’s  must first be determined.

The  is an ordered list of  through which the event passes. This propagation path reflects the hierarchical tree structure of the document. The last item in the list is the , 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  has been determined, the event object passes through one or more . There are three event phases: ,  and . 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  attribute is set to false, the bubble phase will be skipped, and if  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  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 . This phase is also known as the at-target phase. If the  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 . This phase is also known as the bubbling phase.

 

 

扩展阅读

 

转载于:https://www.cnblogs.com/chucklu/p/10672017.html

你可能感兴趣的文章
vector引用参数
查看>>
[转载]typedef 的用法
查看>>
NTC温度采集之数据拟合——freemat软件实现
查看>>
maven私服nexus3.9安装配置
查看>>
U盘出现大量乱码文件,并且不能彻底删除
查看>>
UEditor添加一个普通按钮及其他使用注意事项
查看>>
C语言的第一次实验报告
查看>>
spring JDBC 批量插入数据
查看>>
状态压缩题目小结
查看>>
Android WebView 开发具体解释(三)
查看>>
2016-2017-2 20155325实验二《Java面向对象程序设计》实验报告
查看>>
POJ.3145.Common Substrings(后缀数组 倍增 单调栈)
查看>>
BZOJ.1935.[SHOI2007]Tree园丁的烦恼(CDQ分治 三维偏序)
查看>>
c++可变参数(示例)
查看>>
4923: [Lydsy1706月赛]K小值查询 平衡树 非旋转Treap
查看>>
MySQL 配置文件详解
查看>>
maven 中央仓库地址
查看>>
实现页面查看xml或json数据类似控制台效果
查看>>
Kali配置教程
查看>>
Leetcode: Combination Sum IV && Summary: The Key to Solve DP
查看>>