Home Web Front-end JS Tutorial Detailed explanation of the use of event delegation in JS

Detailed explanation of the use of event delegation in JS

May 09, 2018 am 10:11 AM
javascript use Detailed explanation

This time I will bring you a detailed explanation of the use of event delegation in JS. What are the precautions when using event delegation in JS. The following is a practical case, let's take a look.

Event delegation (also called Event proxy), in fact, this problem is also simple. To understand event delegation, we must first understand the event bubbling mechanism. clear. Give an example of event bubbling:

<ul>
    <li>点击</li>
</ul>
<script>
    var ul=document.getElementsByTagName('ul')[0];
    var li=document.getElementsByTagName('li')[0];
    ul.addEventListener('click', function(){
      alert('我是ul,我被点击了');
    }, false);
    li.addEventListener('click', function(){
      alert('我是li,我被点击了');
    }, false);
</script>
Copy after login

In this code, when we click li, the click event of li is triggered, but at this time, the click event of ul is also triggered. This is The bubbling of events. After understanding this, we can talk about event delegation. Since events can bubble up from the parent element (ul) of the child element (li), then we can add a click event to ul itself and combine all li events. All are delegated to our parent (ul). Maybe some friends here still don’t understand the use of this event delegation. Let’s give an example of event delegation to illustrate:

<ul>
    <li>点击1</li>
    <li>点击2</li>
    <li>点击3</li>
    <li>点击4</li>
    <li>点击5</li>
</ul>
<script>
    //使用事件委托的代码
    var ul=document.getElementsByTagName('ul')[0];
    ul.addEventListener('click', function(e){
      alert(e.target.innerHTML);
    }, false);
    //不使用事件委托,循环给li添加click事件
    var li=document.getElementsByTagName('li')
    for(var i=0;i<li.length;i++){
      li[i].onclick=function(){
        alert(this.innerHTML);
      }
    }
</script>
Copy after login

We are above The code delegates the event to ul, and only adds click events to ul. When running the corresponding li in the browser, the innerHTML corresponding to the li will pop up. This function(e){};## The e parameter in # is actually some event information sent to us by the system when we click on the li. e.target actually refers to the li we click on. Here, every time we click What pops up is the innerHTML of the currently clicked object. This is a simple case of event delegation.

Event delegation is of great significance to us in optimizing the code. We all know that frequent DOM operations are very performance-intensive. Now there are 5 li in ul. If we do not use event delegation to implement the above code To do this, you need to use a for loop and write a click event for each li. In this way, there will be more dom operations. What if there are 10 li, 100 or even more, not to mention the impact of dom operations alone. Performance, if there are too many for loops in li, they will take up a lot of events. If you use event delegation, you can avoid the performance consumption of for loops and the performance consumption of numerous DOM operations.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed explanation of the steps to use WebUploader in the fuzzy box

Detailed explanation of computed use cases in Vue.js

The above is the detailed content of Detailed explanation of the use of event delegation in JS. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What software is crystaldiskmark? -How to use crystaldiskmark? What software is crystaldiskmark? -How to use crystaldiskmark? Mar 18, 2024 pm 02:58 PM

What software is crystaldiskmark? -How to use crystaldiskmark?

Detailed explanation of obtaining administrator rights in Win11 Detailed explanation of obtaining administrator rights in Win11 Mar 08, 2024 pm 03:06 PM

Detailed explanation of obtaining administrator rights in Win11

How to download foobar2000? -How to use foobar2000 How to download foobar2000? -How to use foobar2000 Mar 18, 2024 am 10:58 AM

How to download foobar2000? -How to use foobar2000

How to use NetEase Mailbox Master How to use NetEase Mailbox Master Mar 27, 2024 pm 05:32 PM

How to use NetEase Mailbox Master

How to use Baidu Netdisk app How to use Baidu Netdisk app Mar 27, 2024 pm 06:46 PM

How to use Baidu Netdisk app

Detailed explanation of division operation in Oracle SQL Detailed explanation of division operation in Oracle SQL Mar 10, 2024 am 09:51 AM

Detailed explanation of division operation in Oracle SQL

BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? Apr 26, 2024 am 09:40 AM

BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange?

Teach you how to use the new advanced features of iOS 17.4 'Stolen Device Protection' Teach you how to use the new advanced features of iOS 17.4 'Stolen Device Protection' Mar 10, 2024 pm 04:34 PM

Teach you how to use the new advanced features of iOS 17.4 'Stolen Device Protection'

See all articles