Technical principles of JavaScript event delegation

Source: Internet
Author: User
Tags event listener tag name

One of the hottest technologies in Today's JavaScript technology World is ' event delegation '. Using Event delegation techniques allows you to avoid adding event listeners to a particular node, and instead, event listeners are added to their parent elements. The event listener parses events bubbling up from child elements to find out which child element is the Event. The basic concept is simple, but there are still a lot of people who don't understand how event delegates Work. Here I will explain how event delegation works and provide examples of basic event delegates for a few pure javascript.

Suppose we have an UL element that has several child elements:

<ulId="parent-list"><liId="post-1">item 1</li><liId="post-2">item 2</li><liId="post-3">item 3</li><liId="post-4">item 4</li><liId= "post-5" >item 5 </li><li id = "post-6 ">item 6 </li></ul>  

One of the hottest technologies in Today's JavaScript technology World is ' event delegation '. Using Event delegation techniques allows you to avoid adding event listeners to a particular node, and instead, event listeners are added to their parent elements. The event listener parses events bubbling up from child elements to find out which child element is the Event. The basic concept is simple, but there are still a lot of people who don't understand how event delegates Work. Here I will explain how event delegation works and provide examples of basic event delegates for a few pure javascript.

Suppose we have an UL element that has several child elements:

<ulId="parent-list"><liId="post-1">item 1</li><liId="post-2">item 2</li><liId="post-3">item 3</li><liId="post-4">item 4</li><liId= "post-5" >item 5 </li><li id = "post-6 ">item 6 </li></ul>  

We also assume that when each child element is clicked, it will have its own different events Occurring. You can li add event listeners to each individual element, but sometimes these li elements may be deleted, there may be new additions, and listening to their new or deleted events will be a nightmare, especially if the code for your listener event is placed in another place in the app. But what if you put the listener on their parent element? How can you tell if the child element was clicked?

Simple: when the event of a child element bubbles to the parent ul element, you can check the target property of the event object and capture a reference to the node element that is actually clicked. Here is a very simple JavaScript code that demonstrates the process of event delegation:

Find parent element, Add Listener ... document.getElementById("parent-list").AddEventListener("click",function(e){E.target is the element to be clicked!If the Li element is clickedIf(e. Target&& E. Target. nodeName=="LI"{//find target, output id! Console. Log ( "List item" ,e< Span class= "token punctuation" >.target.id.replace ( "post-" " , "was clicked!" ;} })     

The first step is to add an event listener to the parent Element. When there are events that trigger the listener, check the source of the event and exclude non- li child element Events. If it's an li element, we'll find the target! If it is not an li element, the event is Ignored. This example is very simple, UL and li is a standard parent-child Collocation. Let's experiment with some of the more diverse elements that Match. Let's say we have a parent element with a div lot of child elements, but what we care about is a tag with the "classA" CSS class in It:

Get parent element div, Add Listener ... document.getElementById("mydiv").AddEventListener("click",function(e){E.target is the clicked elementIf(e. Target&& E. Target. nodeName==A){Get CSS class namevar classes= E. Target. className.Split(" ");Search matches!If(classes){For every CSS class the element has ...For(var x=0; X< classes. length; X++){If it has the CSS class we want ...if (classes[x ==  "classA" ) {//bingo!consolelog ( "Anchor element clicked! " ; Now does something here .... }}}}})             

The above example not only compares the tag name, but also compares the CSS class Name. Although a little more complicated, but still very representative. For example, If there is a tag in a tag span , this span will be the target Element. At this point, we need to get back to the DOM tree structure and find out if there is a A.CLASSA element in it.

Because most programmers use a tool library such as jquery to handle DOM elements and events, I recommend that you use the event delegation method inside, because the tool library here provides advanced delegation methods and element screening METHODS.

Hopefully this article will help you understand the behind-the-scenes principles of JavaScript event delegation, and hopefully you will also feel the power of event delegation!

Technical principles of JavaScript event delegation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.