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!
(English: Davidwalsh.) Transferred from: http://www.webhek.com/event-delegate
Technical principles of JavaScript event delegation