This article mainly shares with you the detailed explanation of JS event delegation examples. Let’s take a look at the analysis first. Event delegation: also called event proxy, it uses the principle of bubbling to add events to the parent to trigger the execution effect.
First of all, you must have written such a program. There is a list. When the mouse moves into each li, the background color turns red, so we wrote this code:
(The code I give is usually the key code, you can write the html by yourself.)
window.onload = oUl = document.getElementById('ull' aLi = document.getElementsByTagName('li'); ( i =0;i <p style="margin:10px auto;color:rgb(51,51,51);font-family:verdana, Arial, Helvetica, sans-serif;font-size:14px;background-color:rgb(255,255,255);"> Of course, there is no problem if you look at the code like this. You can add events to each li through a loop, but think about it. If we have many li, do we need to add many events? This is actually very performance consuming. Then we will wonder if we can achieve it by just adding one event. Of course it is possible, otherwise I wouldn’t be talking about it. </p><p style="margin:10px auto;color:rgb(51,51,51);font-family:verdana, Arial, Helvetica, sans-serif;font-size:14px;background-color:rgb(255,255,255);"> That is event delegation through the bubbling principle. We can add events only to the parent oUL, so that no matter which li is moved in, the parent's move-in event will be triggered. (For those who don't understand bubbling, You can refer to my JS bubbling article), but there is also a problem at this time, because my requirement is to change the color of the corresponding li, not the entire list. How does it know which LI I moved the mouse into? At this time One attribute in the universal event object is about to appear, which is the event source (regardless of which element the event is bound to, it refers to the target that actually triggers the event), which can get the LI where your current mouse is located, </p><p style="margin:10px auto;color:rgb(51,51,51);font-family:verdana, Arial, Helvetica, sans-serif;font-size:14px;background-color:rgb(255,255,255);">However, this has compatibility issues. IE is different from the standard. The standard refers to the newer versions of browsers. </p><p style="margin:10px auto;color:rgb(51,51,51);font-family:verdana, Arial, Helvetica, sans-serif;font-size:14px;background-color:rgb(255,255,255);">IE: window.event.srcElement<br>Standard: event. target</p><p style="margin:10px auto;color:rgb(51,51,51);font-family:verdana, Arial, Helvetica, sans-serif;font-size:14px;background-color:rgb(255,255,255);">So we need to make compatibility, which is also very simple. </p><p style="margin:10px auto;color:rgb(51,51,51);font-family:verdana, Arial, Helvetica, sans-serif;font-size:14px;background-color:rgb(255,255,255);">Look at the overall code below: </p><pre style="margin-bottom:0px;padding-right:0px;padding-left:0px;white-space:pre-wrap;font-family:'Courier New';" class="brush:php;toolbar:false;">window.onload = function(){ var oUl = document.getElementById('ull'); var aLi = document.getElementsByTagName('li'); oUl.onmouseover = function(ev){ var event = ev||window.event; // 获取event对象 var target = ev.target || ev.srcElement; // 获取触发事件的目标对象 if(target.nodeName.toLowerCase() == 'li'){ //判断目标对象是不是li target.style.background = 'red'; } } 代码中加了一个标签名的判断,主要原因是如果不加判断,当你鼠标移入到父级oUL上面的时候,整个列表就会变红,这不是我们想要的结果,所以要判断一下。
target.nodeName 弹出的名字是大写的,所以需要转换大小写再比较。
The entire requirement is completed in this way, and the performance is greatly improved when there are many lists.
In fact, event delegation has a second advantage: newly added elements will also have previous events
Suppose we have another requirement, click a button , you can create another li in the list. At this time, the general method is that because the newly created li does not have an event added, it does not have the function of moving in and turning red. However, using the event delegation method, the new li also has this event. . The principle is also very easy to apply, because the event is added to the father. If the father is there, the event is there. You can test it yourself.
Related recommendations:
What are events in js Bubbling and event capturing and event delegation
Comparative analysis of event capture, bubbling and event delegation
Detailed explanation of Javascript event delegation
The above is the detailed content of Detailed explanation of JS event delegation examples. For more information, please follow other related articles on the PHP Chinese website!

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version
Recommended: Win version, supports code prompts!
