当前位置:网站首页>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 .
边栏推荐
- Multi data source switching
- Wechat applet (anti shake, throttling), which solves the problem that users keep pulling down refresh requests or clicking buttons to submit information; Get the list information and refresh the data
- Compiler introduction
- Vodak software: Smart City solution
- Today, I sorted out some problems about high collapse
- Based on if nesting and function call
- Gan, why '𠮷 𠮷'.Length== 3 ??
- 对需求的内容进行jieba分词并按词频排序输出excel文档
- 数据平台下的数据治理
- [training Day11] Nescafe [greed]
猜你喜欢
![[training Day12] tree! Tree! Tree! [greed] [minimum spanning tree]](/img/46/1c7f6abc74e11c4c2e09655aade223.png)
[training Day12] tree! Tree! Tree! [greed] [minimum spanning tree]

Wechat card issuing applet source code - automatic card issuing applet source code - with flow main function

Xiaobai programmer's fifth day

【集训DAY12】X equation 【高精度】【数学】

The third day of Xiaobai programmer

Use of hyperlinks
![[training day15] good name [hash]](/img/62/5cd354e63aab861bf8fa1f265b6986.png)
[training day15] good name [hash]

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

Selenium basic use and use selenium to capture the recruitment information of a website (continuously updating)

H5 lucky scratch lottery free official account + direct operation
随机推荐
分享两个音乐播放地址
The third day of Xiaobai programmer
Ffmpeg plays audio and video, time_ Base solves the problem of audio synchronization and SDL renders the picture
英文术语对应的解释
Interpretation of the source code of all logging systems in XXL job (line by line source code interpretation)
Platform architecture construction
对需求的内容进行jieba分词并按词频排序输出excel文档
Mitsubishi FX PLC free port RS command realizes Modbus Communication
Math programming classification
Some summary about function
Usage of in in SQL DQL query
Compiler introduction
ThreadLocal 总结(未完待续)
Data governance under data platform
JS interview questions
Synchronized and volatile
Data quality: the core of data governance
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
xxl-job中 关于所有日志系统的源码的解读(一行一行源码解读)
[training day13] out race [mathematics] [dynamic planning]