当前位置:网站首页>完善的js事件委托
完善的js事件委托
2022-07-04 17:07:00 【欲饮琵琶码上催】
function eventDelegate(element, eventType, selector, fn) {
element.addEventListener(eventType, e => {
let el = e.target;
// 当点击的元素不为li本⾝的时候,⽐如说span,就找⽗级是否为li
while (!el.matches(selector)) {
// 不断向上找⽗级元素,判断是否有li标签
if (element === el) {
// 直到找到代理元素就不执⾏委托
el = null;
break;
}
el = el.parentNode;
}
el && fn.call(el, e, el);
})
return element;
}
eventDelegate(ul, 'click', 'li', function (e, el) {
console.log(`点击了${
this}标签`)
})
ul.addEventListener('click', function() {
let el = e.target
while (el && !el.matches(selector)) {
el = el.parentNode
if (element === el) {
el = null
}
}
if (el) {
console.log('执行回调函数')
}
}
参考文章
https://zhuanlan.zhihu.com/p/26986963
边栏推荐
- . Net ORM framework hisql practice - Chapter 2 - using hisql to realize menu management (add, delete, modify and check)
- Large scale service exception log retrieval
- Unity makes revolving door, sliding door, cabinet door drawer, click the effect of automatic door opening and closing, and automatically play the sound effect (with editor extension code)
- 6.26CF模拟赛E:价格最大化题解
- 字节跳动Dev Better技术沙龙成功举办,携手华泰分享Web研发效能提升经验
- NBA赛事直播超清画质背后:阿里云视频云「窄带高清2.0」技术深度解读
- [HCIA continuous update] WAN technology
- 蓝桥:合根植物
- 78 year old professor Huake impacts the IPO, and Fengnian capital is expected to reap dozens of times the return
- How is the entered query SQL statement executed?
猜你喜欢

. Net ORM framework hisql practice - Chapter 2 - using hisql to realize menu management (add, delete, modify and check)

vbs或vbe如何修改图标

输入的查询SQL语句,是如何执行的?

TorchDrug教程
![[mathematical modeling of graduate students in Jiangxi Province in 2022] analysis and code implementation of haze removal by nucleation of water vapor supersaturation](/img/da/d46cca19f34223d29003be2e33aaa4.png)
[mathematical modeling of graduate students in Jiangxi Province in 2022] analysis and code implementation of haze removal by nucleation of water vapor supersaturation

力扣刷題日記/day6/6.28

Wireshark抓包TLS协议栏显示版本不一致问题

How is the entered query SQL statement executed?

ISO27001 certification process and 2022 subsidy policy summary

蓝桥:合根植物
随机推荐
提升复杂场景三维重建精度 | 基于PaddleSeg分割无人机遥感影像
Is it science or metaphysics to rename a listed company?
VMware Tools和open-vm-tools的安装与使用:解决虚拟机不全屏和无法传输文件的问题
The block:usdd has strong growth momentum
TCP waves twice, have you seen it? What about four handshakes?
学习路之PHP--phpstudy创建项目时“hosts文件不存在或被阻止打开”
力扣刷题日记/day3/2022.6.25
Scala基础教程--20--Akka
一种将Tree-LSTM的强化学习用于连接顺序选择的方法
With the stock price plummeting and the market value shrinking, Naixue launched a virtual stock, which was deeply in dispute
[go language question brushing chapter] go conclusion chapter | introduction to functions, structures, interfaces, and errors
File processing examples of fopen, FREAD, fwrite, fseek
谷粒商城(一)
[2022 Jiangxi graduate mathematical modeling] curling movement idea analysis and code implementation
With an estimated value of 90billion, the IPO of super chip is coming
The controversial line of energy replenishment: will fast charging lead to reunification?
Imitation of numpy 2
【2022年江西省研究生数学建模】水汽过饱和的核化除霾 思路分析及代码实现
What if the self incrementing ID of online MySQL is exhausted?
[system disk back to U disk] record the operation of system disk back to U disk