1. What is event delegation?Event delegation is also called event proxy. It is to use event bubbling to bind all events of child elements to parent elements. If the child element prevents the event from bubbling, then delegation cannot be achieved. Here is a simple example: For example, if a courier has 100 parcels to deliver to 100 students, it will take a long time to deliver them one by one. At the same time, each student needs to queue up to receive it, which takes a long time. How should it be done? At this time, the courier can entrust the 100 express parcels to the class teacher, who will put them in the office and the students can pick them up after class. In this way, the courier saves time and it is more convenient for students to receive the packages. This process is a delegation event. 2. The principle of event delegationInstead of setting an event listener for each child node individually, the event listener is set on its parent node, and then the bubbling principle is used to affect the setting of each child node. Let’s take a look at how it is implemented in a specific program! The code looks like this: <body> <ul> <li>111111</li> <li>222222</li> <li>333333</li> <li>444444</li> <li>555555</li> </ul> <script> var li = document.querySelectorAll('li'); for(var i=0;i<li.length;i++){ li[i].onclick = function(){ this.style.color = 'green'; } } </script> </body> The running results are: This method can indeed realize our click operation, but in this process, since the click event must be added to li every time, it causes too many DOM accesses, which will prolong the interaction readiness time of the entire page. So, here, we can use event delegation, that is, register the click event for ul, and then use the target of the event object to find the currently clicked li. Because when li is clicked, the event will bubble to ul, and if ul has a registered event, the event listener will be triggered. The implementation code is: <script> var ul = document.querySelector('ul'); ul.addEventListener('click',function(e){ e.target.style.color = 'orange'; }) </script> The running results are: Successfully displayed. 3. The role of event delegationThrough the above operations, we can get: in event delegation, we only operate DOM once, which greatly improves the performance of the program. This is the end of this article about the principles of JavaScript event delegation. For more relevant JavaScript event delegation content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Detailed explanation of the solution to font blur when using transform in CSS3
>>: HTML implementation of a simple calculator with detailed ideas
Table of contents 1. Introduction 1. Component da...
Is it the effect below? If so, please continue re...
This article uses an example to describe how to v...
1. Use contrasting colours. The contrast here ref...
This article records the installation and configu...
Today I was asked what the zoom attribute in CSS ...
Preface We often say that node is not a new progr...
1. Going around in circles After going around in ...
There are many tools, components and programs for...
Table of contents sequence 1. Centralized routing...
I have never used apache. After I started working...
The specific code is as follows: /*Scroll bar wid...
This article shares the MySQL 5.7 installation an...
As a technical novice, I am recording the process...
<br />Table is an awkward tag in XHTML, so y...