1. Event Flow In 1. Concept The event propagation process is 2. DOM event flowDOM events also have a process. There are three stages from event triggering to event response.
In the above example, the process of turning on the computer is like the event flow in Now that we understand event sourcing, let’s look at its three processes! Event Capture:
Event capture is the first step in the event flow. Target phase: When the event reaches the target node, the event enters the target phase. The event is triggered on the target node. Event bubbling: Event bubbling is in the opposite order of event capturing. The order of event capture is from outside to inside, and event bubbling is from inside to outside. Let's look at an example. Clicking box3 will trigger the click events of box2 and box1. <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>JavaScript Event Bubbling</title> </head> <style type="text/css"> #box1 { background: blueviolet;} #box2 {background: aquamarine;} #box3 {background: tomato;} div { padding: 40px; margin: auto;} </style> <body> <div id="box1"> <div id="box2"> <div id="box3"></div> </div> </div> <script> window.onload = function () { const box1 = document.getElementById('box1') const box2 = document.getElementById('box2') const box3 = document.getElementById('box3') box1.onclick = sayBox1; box2.onclick = sayBox2; box3.onclick = sayBox3; function sayBox3() { console.log('You clicked the innermost box'); } function sayBox2() { console.log('You clicked the middle box'); } function sayBox1() { console.log('You clicked the outermost box'); } } </script> </body> </html>
Modern browsers all start capturing events from Now that we know the three stages of event flow, what can we do with this feature? 2. Event delegation Imagine a scenario where you have a bunch of <li> tags under a <ul id="myUl"> <li>item 1</li> <li>item 2</li> <li>item 3</li> ... </ul> It may still be a little hard to understand. To put it simply, it is to use event bubbling to delegate events on an element to its parent. To give an example from real life, on Double Eleven, express deliveries arrive. The courier usually delivers the express to every household door-to-door, which is inefficient. The courier came up with an idea to put all the express deliveries in a community in a courier station inside the community and delegate the delivery of the express. The recipients in the community can go to the courier station to pick up their express deliveries using the pickup code. But what are the benefits of doing this? 1. Advantages of event delegationEvent delegation has two benefits
Reduce memory consumption and optimize page performance In In the above example of binding the Dynamically bind events: If the child element is uncertain or dynamically generated, you can monitor the parent element instead of monitoring the child element. By using event delegation, you don't have to operate on each Now that we know the advantages of event delegation, how do we use it? 2. Use of event delegation The use of event delegation requires usage: element.addEventListener(eventType, function, useCapture);
The third parameter
Take a look at the following example: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>JavaScript Event Delegation</title> </head> <body> <ul> <li>item 1</li> <li>item 2</li> <li>item 3</li> <li>item 4</li> </ul> <script> const myUl = document.getElementsByTagName("ul")[0]; myUl.addEventListener("click", myUlFn); function myUlFn(e) { if (e.target.tagName.toLowerCase() === 'li') { // Determine whether it is the element to be clicked console.log(`You clicked ${e.target.innerText}`); } } </script> </body> </html>
Event bubbling is sometimes really useful, but sometimes it can be annoying. Can you cancel it when you don't need it? 3. Prohibit event bubbling and capture
To disable bubbling and capturing, you can use the It is also very simple to use, with no return value and no parameters. event.stopPropagation(); Please see the following example, which is a slight modification of the above event bubbling example. <div id="box1"> <div id="box2"> <div id="box3"></div> </div> </div> <script> const box1 = document.getElementById('box1') const box2 = document.getElementById('box2') const box3 = document.getElementById('box3') box1.onclick = sayBox1; box2.onclick = sayBox2; box3.onclick = sayBox3; function sayBox3() { console.log('You clicked the innermost box'); } function sayBox2(e) { console.log('You clicked the middle box'); e.stopPropagation(); //Disable event capture and bubbling} function sayBox1() { console.log('You clicked the outermost box'); } </script> When the event bubbles to This is the end of this article about JavaScript event capture bubbling and capture details. For more relevant JavaScript event capture bubbling and capture content, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! IV. References MDN Chinese version https://developer.mozilla.org/zh-CN/ You may also be interested in:
|
<<: 14 Ways to Create Website Content That Engages Your Visitors
>>: Web page html special symbols html special characters comparison table
This article shares with you how to connect pytho...
To achieve this effect, you must first know a pro...
Download the zip installation package: Download a...
Many times, we expect the query result to be at m...
This article shares the specific code for JavaScr...
Table of contents Problem Description Cause Analy...
1. Create a MySQL database 1. Create database syn...
A certain distance can be set between cells in a ...
1. The mysqldump backup method uses logical backu...
SQL paging query:background In the company's ...
1. Introduction to MySQL permissions There are 4 ...
View the installation information of mysql: #ps -...
Writing method 1: update sas_order_supply_month_p...
Table of contents 1. Basic overview of the proces...
This article mainly introduces the analysis of th...