Home > Web Front-end > JS Tutorial > body text

Native JS and jQuery example code sharing about event delegation in JavaScript

黄舟
Release: 2017-07-22 17:06:48
Original
1301 people have browsed it

The following editor will bring you an example of JavaScript event delegation (with native js and jQuery code). The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor to take a look.

The principle of event delegation relies on event bubbling. You can determine which child element triggered the event through event delegation to the parent element to perform a series of operations.

Advantages of using event delegation

1. There is no need to go through the child elements one by one when operating them, they can be triggered based on events The corresponding operation is performed on the object

dom structure is as follows:


<ul id = "oUl">
 <li class = "item"></li>
 <li class = "item"></li>
 <li class = "item"></li>
 <li class = "item"></li> 
 <li class = "item"></li>
</ul>
Copy after login

When li is clicked, Print the value of this li.

Before we learn event delegation, we will traverse all li and add a click event to them, such as this:


var aLi = document.getElementsByTagName(&#39;li&#39;);
for(var i = 0; i < aLi.length; i++) // 遍历li
 aLi[i].addEventListener(&#39;click&#39;, function() { //给每个li添加事件
 console.log(this.innerHTML); 
 });
Copy after login

After learning event delegation, the js native code is as follows:


var oUl = document.getElementById(&#39;oUl&#39;);
oUl.addEventListener(&#39;click&#39;, function(ev) {
 ev = ev||window.event;
 var tag = ev.target; // 触发事件的对象保存在事件的target里面
 console.log(tag.innerHTML);
})
Copy after login

The jQuery code is as follows:


$(&#39;#oUl&#39;).on(&#39;click&#39;, &#39;.item&#39;, function() { 
 console.log($(this).html()); // this指向oUl中触发了click事件并且class为item的子元素
})
Copy after login

In contrast, event delegation only needs to obtain the parent element and does not need to traverse li, which improves the efficiency a lot.

2. After the event is delegated to the parent element, dynamically created (deleted) child elements do not need to rebind (unbind) the event, achieving synchronous updates of elements and events

In the past js event monitoring, child elements dynamically created with js did not have events, and events had to be re-binded for them. However, using event delegation does not require so much trouble. Rebinding events can still implement event monitoring.

Of course, event binding also has disadvantages, because it relies on event bubbling. If bubbling is not supported, event binding cannot be implemented, but I think the probability of this is still low. There is also the misjudgment of events. For example, the function of button1 and button2 on the page is to pop up the value when clicked, while the function of button3 is to change the color of the page when clicked. The functions of the same event of these three buttons are different. When you change click If events are delegated to their common parent element, event misjudgment will occur.

So I think event delegation occurs when the event functions of a sub-collection are the same. If they are not the same, do not use event delegation to avoid self-defeating.

In actual development, mastering event binding will improve the standardization and efficiency of the code. Generally speaking, the advantages outweigh the disadvantages.

The above is the detailed content of Native JS and jQuery example code sharing about event delegation in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template