当前位置:网站首页>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 .
边栏推荐
- Three tips from small video app source code developers on small video platforms
- [high concurrency, high performance and high availability of massive data MySQL practice-10] - Implementation of mvcc in InnoDB
- How to become a senior digital IC Design Engineer (6-4) digital IC Verification: test point decomposition
- 网上联系客户经理办理炒股开户安全吗?
- [apprendre la programmation FPGA - 49 à partir de zéro]: vision - Comment la puce a - t - elle été conçue?
- 【Redis】Key的层级结构
- 想请教一下,我在中山,到哪里开户比较好?在线开户是安全么?
- How to become a senior digital IC Design Engineer (5-1) theory: clock technology, reset Technology
- Written examination questions, answers and analysis of Digital IC design and FPGA design (2) 2021 Huawei Hisilicon (Part 1)
- Analysis of advantages and disadvantages of environment encryption and transparent encryption
猜你喜欢

How to become a senior digital IC Design Engineer (4-2) script: file read / write operation realized by Verilog HDL code

如何成为一名高级数字 IC 设计工程师(4-3)脚本篇:C 语言实现的文件读写操作

The left toolbar of hbuilder is missing

Dialogue with opensea co creation Alex: we still only touch the tip of the iceberg of NFT capability | chain catcher

How to prevent virus

PR FAQ: how to retrieve accidentally deleted video and audio in PR?

How to use PN junction to measure temperature?

Large scale visual relationship understanding

如何成为一名高级数字 IC 设计工程师(4-2)脚本篇:Verilog HDL 代码实现的文件读写操作

OculusRiftS与Unity.UI的交互(1)-总览
随机推荐
Large scale visual relationship understanding
Scala 基礎 (三):運算符和流程控制
How to become a senior digital IC Design Engineer (4-2) script: file read / write operation realized by Verilog HDL code
【Redis】初识 Redis
一种全面屏手势适配方案
Adding, deleting, checking and modifying stack - dynamic memory
Which brokerage is safer and more convenient to open an account for compass mobile stock?
A full screen gesture adaptation scheme
[C language] Fibonacci sequence and frog jumping steps (the most detailed elementary frog jumping steps)
Written examination questions, answers and analysis of Digital IC design and FPGA design (2) 2021 Huawei Hisilicon (Part 1)
Use kubernetes resource lock to complete your own ha application
指南针手机股票开户哪个券商更安全更方便?
【Redis】Set类型
同花顺究竟是如何开户?现在网上开户安全么?
[image detection] recognition of the front and back of a coin based on texture features with matlab code attached
How to become a senior digital IC Design Engineer (6-5) digital IC Verification: coverage collection
同花顺究竟是如何开户?在线开户是安全么?
Blog publishing test 3
Kuboardv3 and monitoring kit installation
[redis] data introduction & General Command & string type