当前位置:网站首页>e. Difference between target and e.currenttarget
e. Difference between target and e.currenttarget
2022-06-29 02:04:00 【wam923259736】
stay DOM There are two properties in the event object that always bother me from time to time , Namely target and currentTarget, Sometimes I'm confused about the difference between the two , Therefore, it is necessary to sort out these two attributes , Deepen the understanding , For future query .
MDN Chinese vs target The explanation of is , A reference to an object that triggers an event , When an event handler is called during the bubble or capture phase of an event .
And for currentTarget, It refers to when the event traverses DOM when , Identifies the current target of the event . It always refers to the element to which the event handler is attached , instead of event.target, It identifies the element where the event occurred .
Let me give you an example .
Event bubbling stage
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<ul>
<li>hello 1</li>
<li>hello 2</li>
<li>hello 3</li>
<li>hello 4</li>
</ul>
<script> let ul = document.querySelectorAll('ul')[0] let aLi = document.querySelectorAll('li') ul.addEventListener('click',function(e){
let oLi1 = e.target let oLi2 = e.currentTarget console.log(oLi1) // Clicked li console.log(oLi2) // ul console.og(oLi1===oLi2) // false }) </script>
</body>
</html>
We know ,e.target Can be used to implement event delegation , The principle is bubbling through events ( Or event capture ) Add event listeners to the parent element ,e.target Point to the element that triggers the event , As in the above example ,e.target Point to what the user clicked li, Bubbling with Events ,li Click on the event bubble to ul On , By giving ul Add listening events to each li Add the effect of listening Events , and e.currentTarget It refers to the object listening for the binding event , namely ul, From here we can find ,e.currentTarget=this return true, and e.target=this return false.e.currenttarget and e.target It's not equal .
边栏推荐
- 【Redis】List类型
- 直播预告|SQL也能玩转工业级机器学习?MLOps meetup V3带你一探究竟!
- Query any field of any table in JPA to the util method of entity class dto
- 110. simple chat room 13: chat room server
- [high concurrency, high performance and high availability of massive data MySQL practice-10] - Implementation of mvcc in InnoDB
- 如何成为一名高级数字 IC 设计工程师(5-1)理论篇:时钟技术、复位技术
- Application of X6 in data stack index management
- 如何成为一名高级数字 IC 设计工程师(6-4)数字 IC 验证篇:测试点分解
- In MySQL database, the two data written when creating tables with foreign keys are the same. Do I copy them or fail to display them
- Callback function of unity after importing resources
猜你喜欢

Understand flex layout in an article

Business system anti-virus

SQL splits strings into tables

HBuilder左侧工具栏不见了

Magic Quadrant of motianlun's 2021 China Database

Large scale visual relationship understanding

【Redis】List类型

Introduction to UE gameplay 44 (animation import FBX and production standard)
![[redis] sortedset type](/img/7f/f5f1aa603c8994b669d52a435fed7e.png)
[redis] sortedset type
Fundamentals of scala (3): operators and process control
随机推荐
The metadata request parsing principle of OData XML format applied by SAP ui5 is based on domparser
为什么建议大家尽可能选择自研公司
想请教一下,我在中山,到哪里开户比较好?在线开户是安全么?
Live broadcast preview | can SQL also play industrial machine learning? Mlops meetup V3 takes you to the bottom!
Redis data migration (III)
基于 FPGA 的 RISC CPU 设计(4)关于项目的 36 个问题及其答案
[從零開始學習FPGA編程-49]:視野篇 - 芯片是如何被設計出來的?
Finally got the byte offer. The 25-year-old inexperienced experience in software testing is written to you who are still confused
如何成为一名高级数字 IC 设计工程师(3-5)工具篇:SpyGlass 技术
11-Go基础:接口
[redis] list type
如何成为一名高级数字 IC 设计工程师(6-5)数字 IC 验证篇:覆盖率收集
How to become a senior digital IC Design Engineer (4-5) script: file comparison operation implemented by shell script
Blog publishing test 3
[redis] sortedset type
[从零开始学习FPGA编程-49]:视野篇 - 芯片是如何被设计出来的?
How to encrypt anti copy program
[机缘参悟-33]:眼见不一定为实,大多数时候“眼见为虚”
How to use PN junction to measure temperature?
ASP. Design and implementation of net+sql online alumni list