当前位置:网站首页>DOM event object
DOM event object
2022-07-25 22:32:00 【Like the rising sun】
List of articles
The event object will be obtained when the event is triggered , Object contains some information about the current event , For example, click the event to get the clicked location , Keyboard input events can get the pressed key .
1. Get event object
In giving DOM When a node binds an event , You need to pass an event handler , It is essentially a function , Called when the event triggers .
When the event handler is called , By default, a parameter will be passed , This parameter is the event object .
2. Properties and methods under common event objects
2.1 attribute
2.1.1 target
target Indicates the target finally captured by the current event .
<div class="a">
I am the first node a
<div class="b">
I am the second node b
<div class="c">
I am the third node c
<div class="d">
I am the fourth node d
<div class="e">
I am the fifth node e
<div class="f">
I am the innermost element f
</div>
</div>
</div>
</div>
</div>
</div>
<div class="result" style="margin-top: 16px;"></div>
<script> var resultEle = document.querySelector('.result'); document.querySelector('.a').addEventListener('click', function(e) {
resultEle.innerText = ' The captured element class name is ' + e.target.className; }); </script>
You can see that the event is bound to the class named c Node , When clicking on its child nodes , The child node is the element finally captured .
Empathy , Clicking on other nodes will also capture the corresponding element class name
2.1.2 currentTarget
adopt currentTarget You can get the element that triggers the event at present .
<div class="a">
I am the first node a
<div class="b">
I am the second node b
<div class="c">
I am the third node c
<div class="d">
I am the fourth node d
<div class="e">
I am the fifth node e
<div class="f">
I am the innermost element f
</div>
</div>
</div>
</div>
</div>
</div>
<div class="result" style="margin-top: 16px;"></div>
<script> var resultEle = document.querySelector('.result'); document.querySelector('.a').addEventListener('click', function(e) {
resultEle.innerText = [ ' The class name of the element that currently triggers the event is :', e.currentTarget.className, '. The currently captured element class name is :', e.target.className, ].join(''); }); </script>

No matter which child node is clicked ,currentTarget Are nodes that represent the current trigger event .
2.2 Method
2.2.1 stopPropagation
Calling this method will prevent the event from bubbling , Most of the scenarios used are when a parent element and the element itself are bound with the same event .
2.2.2 preventDefault
This method can cancel the default behavior of the event , For example, click on hyperlinks , Jump will occur , Jump action is the default behavior .
Bind click events to hyperlinks , Call preventDefault attribute , The default behavior will be canceled , That is, there will be no jump .
<a href="http://www.baidu.com/"> Baidu </a>
<script> var aTag = document.querySelector('a'); aTag.onclick = function(e) {
e.preventDefault(); alert(' Jump terminated !'); }; </script>

3. Summary
The event object contains information about the event , There are event objects , In order to do deeper interaction optimization for each event .
边栏推荐
- [training Day12] x equation [high precision] [mathematics]
- IPv4 addresses have been completely exhausted, and the Internet can work normally. NAT is the greatest contributor!
- 完啦,上班三个月,变秃了
- 对需求的内容进行jieba分词并按词频排序输出excel文档
- 【集训DAY13】Backpack【动态规划】【贪心】
- torchvision
- 3dslicer importing medical image data
- ML-Numpy
- 3dslicer import cone beam CT image
- Common source code for ArcGIS development
猜你喜欢

Win10 set up a flutter environment to step on the pit diary

LabVIEW 开发 PCI-1680U双端口CAN卡

访问者模式(visitor)模式

【集训DAY13】Internet【并查集】

Xiaobai programmer's sixth day

xxl-job中 关于所有日志系统的源码的解读(一行一行源码解读)

Compiler introduction

According to the use and configuration of data permissions in the open source framework

Naming rules of software test pytest pytest the pre and post confitest of use cases Py customized allure report @pytest.mark.parameter() decorator as data-driven

Wechat official account application development (I)
随机推荐
【集训DAY15】好名字【hash】
Short circuit and &, short circuit or |, logic and &, logic or |; Conditional operator
Synchronized and volatile
Selenium basic use and use selenium to capture the recruitment information of a website (continuously updating)
【集训DAY13】Out race【数学】【动态规划】
Wechat card issuing applet source code - automatic card issuing applet source code - with flow main function
点亮字符串中所有需要点亮的位置,至少需要点几盏灯
3dslicer import cone beam CT image
To light up all the positions in the string that need to be lit, at least a few lights are needed
访问者模式(visitor)模式
Data quality: the core of data governance
【集训DAY13】Backpack【动态规划】【贪心】
ArcGIS10.2配置PostgreSQL9.2标准教程
IFLYTEK smart office book air e-book reader makes my work life healthier
三菱FX PLC自由口RS指令实现MODBUS通讯
【集训DAY15】简单计算【树状数组】【数学】
Perform Jieba word segmentation on the required content and output EXCEL documents according to word frequency
Basic principle of torque motor control
Solve several common problems
[training Day12] min ratio [DFS] [minimum spanning tree]