Introduction
(Supplement: Thanks to the guidance of senior othree, it is pointed out that this is actually talking about the order of event propagation in the DOM, so the title and content are revised. The original title is: JavaScript Event Propagation: Capturing and Bubbling)
Today, we bring you the event propagation mechanism in the DOM, and the code related to these events, I believe everyone should be familiar with, that is addEventListener
, preventDefault
and stopPropagation
.
Simply put, it is the order in which events are propagated in the DOM, and what you can do with these events.
Why is there the term “propagation order”? Suppose you have a ul
element with many li
elements underneath, representing different items. When you click on any li
, you actually click on ul
because ul
wraps all li
.
If I add eventListener
to two elements, which one will be executed first? At this time, it is important to know the execution order of events.
In addition, because the mechanism of some browsers (yes, I am talking about IE) is different, I will not mention those things at all. Those who are interested can study the reference materials attached at the end of the article.