The process of capturing and bubbling events is collectively referred to as the propagation of events
The propagation of events can be prevented:
- In the Stoppropagation () method, use the
- Set cancelbubble = True under IE;
In the process of capturing stoppropagation (), the subsequent bubbling process does not occur ~ cancelbubble (only bubbling, not capturing in IE) 3. The default behavior of blocking events, such as Click <a> Jump ~
- Use the Preventdefault () method in the user's list;
- Set Window.event.returnValue = False under IE;
Not all events can bubble, for example: Blur, focus, load, unload, (this is taken from someone else's article, I did not test)
Two phases of JavaScript for all events: capture and bubbling
It's a simple HTML code.
<body>
<ul>
<li>
Click here
</li>
</ul>
</body>
If we click on Li, the first response to the body of the capture event, followed by the UL Li, and then into the bubble time, in reverse order.
If we set the response function separately, we will respond separately
We can stop bubbles.
Function (e)
{
if (e && e.stoppropagation) //Support Stoppropation () method e.stoppropagation () Else //Otherwise, we have to make use of ie way to cancel event bubbling window.event.cancelBubble = true;
}
In fact, the capture phase is the exact opposite of the bubbling phase, where the event is propagated by the ancestor elements to the child elements, and a pebblessintosthe sinks from the water to the bottom, stating that there is no such stage in the Ie,opera browser. It can be seen from the method of registering event listener provided by each browser, such as attachevent for Ie,opera, with two parameters, Attachevent ("on" +TYPE,FN), The AddEventListener for the so-called Standard browser has three parameters, AddEventListener (Type,fn,boolean), the first two parameters are not explained, the third parameter is Boolean, is to decide whether the registration event occurs in the capture or bubbling phase, as follows:
True: Capture phase
False: Bubbling phase
I think we are more familiar with the concepts of "capture" and "bubbling", because in all the browsers we use, event bubbling is supported, that is, events are propagated to ancestral elements by child elements, just as bubbles float from the bottom to the surface. And in the so-called standard browser such as Firefox,chrome,safari, there is a stage of event propagation, that is, the capture phase, which is rarely useful, so it is inevitable that people are inadvertently forgotten, not commonly used does not mean that it does not exist, in a scientific and rigorous attitude, It is necessary for us to look at it.
Capturing and bubbling