JavaScript event delegation principle

JavaScript event delegation principle

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 delegation

Instead 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!
For example, we now have an unordered list with five li in it. We want to add a click event to each li. At this time, our normal operation is to add a click event to each li through a loop.

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 delegation

Through 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 js event delegation
  • A brief analysis of event delegation examples in JS
  • js to realize web message board function
  • JavaScript to implement web message board function
  • Native JS implementation of message board
  • This article tells you how to use event delegation to implement JavaScript message board function

<<:  Detailed explanation of the solution to font blur when using transform in CSS3

>>:  HTML implementation of a simple calculator with detailed ideas

Recommend

WeChat applet component development: Visual movie seat selection function

Table of contents 1. Introduction 1. Component da...

Example code for implementing background blur effect with CSS

Is it the effect below? If so, please continue re...

Detailed explanation of how to view the number of MySQL server threads

This article uses an example to describe how to v...

Several ways to improve the readability of web pages

1. Use contrasting colours. The contrast here ref...

Definition and function of zoom:1 attribute in CSS

Today I was asked what the zoom attribute in CSS ...

In-depth understanding of the implementation principle of require loader

Preface We often say that node is not a new progr...

Example code for CSS columns to achieve two-end alignment layout

1. Going around in circles After going around in ...

Implementation of react automatic construction routing

Table of contents sequence 1. Centralized routing...

Detailed explanation of the pitfalls of Apache domain name configuration

I have never used apache. After I started working...

Example code for css3 to achieve scroll bar beautification effect

The specific code is as follows: /*Scroll bar wid...

mysql5.7 installation and configuration tutorial under Centos7.3

This article shares the MySQL 5.7 installation an...

Linux CentOS6.9 installation graphic tutorial under VMware

As a technical novice, I am recording the process...

XHTML introductory tutorial: Application of table tags

<br />Table is an awkward tag in XHTML, so y...