当前位置:网站首页>DOM operation of JS - Events
DOM operation of JS - Events
2022-07-20 06:38:00 【M78_ Domestic 007】
event : The element is in a certain state ( The implementation of browser is also called event triggering ) When reached , A pre-set procedure to be executed , We call it event handler
Events are a common means of interacting with users
Three important numbers of events : Event source Event type Event handler (handler)
How events are bound :
1、 Inline : Write in the line of the label , Its attribute value is the code executed when the event is triggered ( Press js Script execution )
Code instance :
<div class="box" onclick="console.log(66666)">
Am I
</div>
The triggered execution code can be a function call :
<div class="box" onclick="myconsole.mylog();fn()">
Am I 1
</div>
<script>
var myconsole = {
mylog: () => {console.log(" I wrote my own function ")}
}
function fn(){
console.log(" I wrote my own function 2")
}
</script>
2、 Attribute binding : Only one event handler can be bound
Code instructions :
<div class="box">
Am I
</div>
<script>
var box = document.querySelector(".box")
box.onclick = function() {
console.log(" The first event ")
}
box.onclick = function() {
console.log(" The second event ")
}
</script>
Running results :
Why only run the second function ?
because box Point attribute , If there is no such attribute, it will be added and assigned , If so, overwrite . Just like object assignment .
3、 a key : Use this as much as possible , It can solve the compatibility problem , It is an asynchronous non blocking function
Add an event listener to the element : Elements .addEventListener(" Event type "," Processing function "), Remember the event type. There is no need to write here on.
An element can be bound to multiple listeners .
<div class="box">
Am I
</div>
<script>
var box = document.querySelector(".box")
box.addEventListener("click",function(){
console.log(111)
})
box.addEventListener("click",function(){
console.log(222)
})
</script>
Running results :
The unbinding method of the event :
For in-line binding and attribute binding, we unbind by manually leaving empty .
Intra row unbinding :
<div class="box" onclick="console.log(66666)">
Am I
</div>
<script>
var box = document.querySelector(".box")
//onclick yes box Inline properties of , The element point attribute can be obtained
box.onclick=null
</script>
Attribute unbinding :
<div class="box">
Am I
</div>
<script>
var box = document.querySelector(".box")
box.onclick = function() {
// After clicking once, you can't click any more
box.onclick=null
console.log(" The first event ")
}
</script>
There is a special method for unbinding the listener ——removeEventListener(" Event type "," Execute function "), If the binding is an anonymous function , Will never be unbound .
Code presentation instructions :
<div class="box">
Am I
</div>
<script>
function fn1(){
console.log(111)
}
function fn2(){
console.log(222)
}
var box = document.querySelector(".box")
box.addEventListener("click",fn1)
box.addEventListener("click",fn2)
box.removeEventListener("click",fn1)
box.removeEventListener("click",fn2)
</script>
边栏推荐
- Fast-lio, fast-lio2 and fast-lio
- DeFi 2.0的LaaS协议Elephant,重振DeFi赛道发展的关键
- J9 digital currency platform popular science: how does the double consensus of Sui network work?
- NahamCon CTF 2022 Babyrev逆向分析
- [exercise C] civil servant interview
- Luogu p1016 [noip1999 improvement group] traveller's budget solution
- mysql数据库引擎分析
- The difference between overloading and rewriting
- 靶机无法获得IP地址, 该怎么办?
- sql注入总结
猜你喜欢
个人开发的解ctf usb的键盘流量的工具 KeyboardTraffic
codeforces每日5题(均1500)-第十九天
The LAAS protocol elephant of defi 2.0 is the key to revitalizing the development of defi track
【C 练习】箭形图案
迭代器和生成器(es6)
NahamCon CTF 2022 Babyrev逆向分析
10天学会STM32的学习心得总结
Arthas下载与启动
Tutoriel de requête SQL pour la science des données avec mon serveur SQL
Solution to expiration of visual studio tomato plug-in -- continue to resume trial
随机推荐
迭代器和生成器(es6)
10天学会STM32的学习心得总结
RMAN backup compression ratio? About 5 times
DOM operation of JS -- event object
shell查询prometheus数据
Is it safe to open Huatai account on your mobile phone?
JS的DOM操作——事件
广发证券怎么样?网上开户安全吗?
React.Context和redux缓存的数据无法跨浏览器Tab共享,怎么解决?
ovirt: api
【开发教程2】开源蓝牙心率防水运动手环-套件检测教程
MySQL 事务
使用 My SQL Server 实现数据科学的 SQL 查询教程
MySQL password modification error 1064 (42000):
JVM知识图谱(更新中)
ENVI_IDL: 创建HDF5文件并写入数据(以将Geotiff文件写入HDF文件为例) + 详细解析
Go语言 变量与数据类型
MySQL string operation (substr interception, locate instr position child parent string judgment)
个人开发的解ctf usb的键盘流量的工具 KeyboardTraffic
WTO官方公布孙宇晨MC12发言全文 涉电子商务等重大议题