当前位置:网站首页>Perfect JS event delegation
Perfect JS event delegation
2022-07-04 18:56:00 【If you want to drink pipa, hurry up】
function eventDelegate(element, eventType, selector, fn) {
element.addEventListener(eventType, e => {
let el = e.target;
// When the clicked element is not li Ben ⾝ When ,⽐ If you say span, Just look for ⽗ Whether level is li
while (!el.matches(selector)) {
// Keep looking up ⽗ Level element , To determine if there is li label
if (element === el) {
// Don't hold on until you find the proxy element ⾏ entrust
el = null;
break;
}
el = el.parentNode;
}
el && fn.call(el, e, el);
})
return element;
}
eventDelegate(ul, 'click', 'li', function (e, el) {
console.log(` Click. ${
this} label `)
})
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(' Execute callback function ')
}
}
Reference article
https://zhuanlan.zhihu.com/p/26986963
边栏推荐
- Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology
- Li Kou brush question diary /day3/2022.6.25
- Lua EmmyLua 注解详解
- 完善的js事件委托
- Halcon模板匹配
- 工厂从自动化到数字孪生,图扑能干什么?
- Scala基础教程--14--隐式转换
- 基于unity的愤怒的小鸟设计
- Nature Microbiology | 可感染阿斯加德古菌的六种深海沉积物中的病毒基因组
- Learning path PHP -- phpstudy "hosts file does not exist or is blocked from opening" when creating the project
猜你喜欢
力扣刷题日记/day5/2022.6.27
Li Kou brush question diary /day2/2022.6.24
Wireshark packet capturing TLS protocol bar displays version inconsistency
Thawte通配符SSL证书提供的类型有哪些
Li Kou brush question diary /day1/2022.6.23
Uni app and uviewui realize the imitation of Xiaomi mall app (with source code)
字节跳动Dev Better技术沙龙成功举办,携手华泰分享Web研发效能提升经验
被忽视的问题:测试环境配置管理
输入的查询SQL语句,是如何执行的?
Scala basic tutorial -- 15 -- recursion
随机推荐
爬虫初级学习
Scala basic tutorial -- 15 -- recursion
6.26cf simulation race e: solution to the problem of price maximization
[mathematical modeling of graduate students in Jiangxi Province in 2022] analysis and code implementation of haze removal by nucleation of water vapor supersaturation
Load test practice of pingcode performance test
Scala basic tutorial -- 18 -- set (2)
What if the self incrementing ID of online MySQL is exhausted?
一直以为做报表只能用EXCEL和PPT,直到我看到了这套模板(附模板)
Scala basic tutorial -- 17 -- Collection
Li Kou brush question diary /day5/2022.6.27
力扣刷题日记/day7/6.30
Scala basic tutorial -- 19 -- actor
一、C语言入门基础
I wrote a learning and practice tutorial for beginners!
Scala basic tutorial -- 20 -- akka
Is it safe to open an account online? is that true?
力扣刷题日记/day5/2022.6.27
机器学习概念漂移检测方法(Aporia)
Scala基础教程--18--集合(二)
Grain Mall (I)