What is event bubbling and capturing
2016-11-18 21:59
330 查看
http://stackoverflow.com/questions/4616694/what-is-event-bubbling-and-capturing
http://www.quirksmode.org/js/events_order.html#
http://javascript.info/tutorial/bubbling-and-capturing#
http://bitovi.com/blog/2010/10/a-crash-course-in-how-dom-events-work.html#
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:
bubble up, trickle down
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.
We can use the
Only the bubbling model is supported by all major browsers. If you are going to use event
capturing anyway, you need to handle event bubbling for IE. This makes it event bubbling easier to use, in that it provides wider browser compatibility. On the other hand, the performance
of event bubbling may be slightly lower for complex DOMs.
In the structure above, assume that a click event occurred in the
In capturing model, the event will be handled by the
(click event handlers in the
fire first), then in the
then at the last in the target element,
In the bubbling model, the opposite will happen: the event will be first handled by the
then by the
and at last by the
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.
http://www.quirksmode.org/js/events_order.html#
http://javascript.info/tutorial/bubbling-and-capturing#
http://bitovi.com/blog/2010/10/a-crash-course-in-how-dom-events-work.html#
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:
bubble up, trickle down
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.
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.
Only the bubbling model is supported by all major browsers. If you are going to use event
capturing anyway, you need to handle event bubbling for IE. This makes it event bubbling easier to use, in that it provides wider browser compatibility. On the other hand, the performance
of event bubbling may be slightly lower for complex DOMs.
Example
<div> <ul> <li></li> </ul></div>[/code]
In the structure above, assume that a click event occurred in the
lielement.
In capturing model, the event will be handled by the
divfirst
(click event handlers in the
divwill
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
divelement.
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 divs = document.getElementsByTagName('div');function capture() { log('capture: ' + this.firstChild.nodeValue.trim()) }function bubble() { log('bubble: ' + this.firstChild.nodeValue.trim()) }for (var i = 0; i < divs.length; i++) { divs[i].addEventListener('click', capture, true); divs[i].addEventListener('click', bubble, false); }var $log = $('#log');function log(msg) { $log.append('<p>' + msg + '</p>'); }[/code]
div { border: 1px solid red; padding: 5px; min-height: 10px; }[/code]
<script src="http://code.jquery.com/jquery.min.js"></script><div>1 <div>2 <div>3 <div>4 <div>5</div> </div> </div> </div></div><section id="log"></section>[/code]
相关文章推荐
- javascript event bubbling and capturing (再谈一谈js的事件冒泡和事件补获,看到这篇文章加深了理解)
- What is Event Driven Architecture (EDA) and Why Does it Matter EDA 摘要
- What is d_type and why Docker overlayfs need it
- What is the relationship between the From, Contact, Via and Record-Route/Route headers?
- What is Split Brain in Oracle Clusterware and Real Application Cluster (文档 ID 1425586.1)
- What is the GlobalSuppressions.cs/GlobalSuppressions.vb file and why is it needed
- What Is /dev/shm And Its Practical Usage
- What is the difference between a URI, a URL and a URN
- What is the difference between a URI, a URL and a URN?
- What is a text file and what is a binary file :)
- What is a NullPointerException, and how do I fix it?
- What is the difference between << and += while injecting elements to Hash.new([])?
- Linux Commands for Beginners--which and whatis commands
- What is the difference between Class Path and Build Path
- What is a Context in Java and ApplicationContext vs WebApplicationContext
- What is Complex Event Processing? (Part 4)
- What is the difference between symbolic link and hard link?
- what is the different between function and method in python
- What is the difference between .NET 1.1,2.0,3.0,3.5 and 4.0
- What is the working set, commit, and private working set memory size?