当前位置:网站首页>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 .
边栏推荐
- Uniapp notes
- Digital IC design, FPGA design written examination questions, answers and analysis of autumn move (1) 2022 Ziguang zhanrui (Part 1)
- 如何成为一名高级数字 IC 设计工程师(4-2)脚本篇:Verilog HDL 代码实现的文件读写操作
- 如何成为一名高级数字 IC 设计工程师(6-6)数字 IC 验证篇:系统级仿真
- Application of X6 in data stack index management
- 扁平结构转换为树结构
- QT basics tutorial: qstringlist
- Server antivirus
- MySQL realizes data comparison between two tables by calculating intersection and difference sets
- HBuilder左侧工具栏不见了
猜你喜欢

The left toolbar of hbuilder is missing

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

Using autogluon to forecast house price

Introduction to UE gameplay 44 (animation import FBX and production standard)

In simple terms, server intrusion prevention

Zhongyi technology resumed the review status of the gem IPO, and xuxiaofei no longer acted as a practicing lawyer

How to become a senior digital IC Design Engineer (4-5) script: file comparison operation implemented by shell script

【内网穿透】Frp 自建跳板-两个内网通讯上线-反弹shell

OculusRiftS与Unity.UI的交互(1)-总览

SAP ui5 beginner tutorial Part 23 - sorting sort and grouping Group trial version of list control
随机推荐
SystemVerilog-结构体(一)
How to become a senior digital IC Design Engineer (6-4) digital IC Verification: test point decomposition
What is stock online account opening? Is it safe to open an account online?
数字 IC 设计、FPGA 设计秋招笔试题目、答案、解析(1)2022 紫光展锐(上)
如何成为一名高级数字 IC 设计工程师(4-2)脚本篇:Verilog HDL 代码实现的文件读写操作
[redis] set type
Callback function of unity after importing resources
【Redis】Set类型
Finally got the byte offer. The 25-year-old inexperienced experience in software testing is written to you who are still confused
Magic Quadrant of motianlun's 2021 China Database
同花顺究竟是如何开户?现在网上开户安全么?
11-Go基础:接口
如何成为一名高级数字 IC 设计工程师(6-7)数字 IC 验证篇:DEBUG 技巧
QT basics tutorial: qstringlist
ASP. Net based on LAN
基于 FPGA 的 RISC CPU 设计(4)关于项目的 36 个问题及其答案
Understand flex layout in an article
Is the ETF fund reliable and safe
Tiflash compiler oriented automatic vectorization acceleration
Scala Foundation (3): Operators and Process Control