Home > Article > Web Front-end > In-depth analysis of js bubbling events_Basic knowledge
When doing DOM operations in JavaScript, you will definitely encounter js bubbling events. The most common one is the div pop-up event as shown in the illustration
When you click on the gray part, the pop-up window disappears, but when you click on the black part, it has no effect.
Use the following code to analyze js bubbling events
html code:
Illustration:
When the button is clicked, "I am button" will pop up and then "I am div" will pop up, because the button event is triggered first and then the next layer div click event is triggered,
The triggering of events is based on the first-in, first-out principle.Illustration:
Then sometimes we don’t want multiple events to trigger and cause conflicts, so events have stopPropagation(); methods to prevent bubbling
There is also an event method that is also commonly used, such as a link. If I don’t want to jump when clicking the link, I use the event.preventDefault(); methodThe example code is as follows