当前位置:网站首页>JS EventListener
JS EventListener
2022-06-27 08:17:00 【SeriousLose】
DOM2 Level event
Event flow can be selected . Multiple events of the same kind can be bound . The event name can form a string .
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="content"></div>
</body>
</html>
addEventListener
Add listening Events
let content = document.getElementById("content"); content.addEventListener("click", function () { console.log("seriousLose"); }, false) content.addEventListener("click", function () { console.log("hello seriousLose"); }, false)
Block default events preventDefault()
document.body.addEventListener('touchmove', function (event) { event.preventDefault(); },false);
Stop the event from bubbling stopPropagation()
<div id="div"><button id="btn"> Button </button></div>
function showType(event) { alert(event.type); event.stopPropagation(); } function showDiv() { alert("div"); } document.getElementById("btn").addEventListener("click", showType); document.getElementById("div").addEventListener("click", showDiv);
removeEventListener
Remove Events
function add() { console.log("I me here seriousLose"); } content.addEventListener("mouseenter", add, false); content.removeEventListener("mouseenter", add, false);
<aside> removeEventListener You need to know which event handler you need to remove . Anonymous function discards its own function name , It can't be removed .
</aside>
content.addEventListener("click", function () {
console.log("hello seriousLose");
}, false)
// It's no use , Cannot be removed
content.removeEventListener("click", function () {
console.log("hello seriousLose");
}, false), false);
<aside> IE9 Following IE Browser does not support addEventListener() and removeEventListener(), Use attachEvent() And detachEvent() Instead of , because IE9 The following is not supported for event capture , So there's no third parameter , The first event name should be preceded by on
</aside>
demos/DW-dom.html at main · SeriousLose/demos
EventTarget.addEventListener() - Web API Interface reference | MDN
EventTarget.removeEventListener - Web API Interface reference | MDN
边栏推荐
- Associated GIS: all roads lead to ue5 City
- Lvgl usage demo and instructions 2
- Index +sql exercise optimization
- 参考 | 升级 Win11 移动热点开不了或者开了连不上
- Linux下Redis的安装
- ServletConfig与ServletContext
- [daily practice] realization of product card animation effect
- SQL Sever column name or number of supplied values does not match the table definition
- Publications under nature, science and cell
- "Short video" Linxia fire rescue detachment carries out fire safety training
猜你喜欢
MySQL lock details
JS to judge the odd and even function and find the function of circular area
Mapping of Taobao virtual product store opening tutorial
【10. 差分】
【13. 二进制中1的个数、位运算】
oracle怎样将字符串转为多行
盲测调查显示女码农比男码农更优秀
Five basic types of redis
Lvgl GUI guide porting code to stm32
Code source AQS sous - jacent pour la programmation simultanée juc
随机推荐
If xn > 0 and X (n+1) /xn > 1-1/n (n=1,2,...), Prove that the series Σ xn diverges
[11. two dimensional difference]
ZABBIX deployment instructions (server+win client + switch (H3C))
MySQL index details
ACM课程学期总结
【10. 差分】
并发编程JUC的AQS底层源码
LVGL GUI GUIDER移植代码到STM32
Five basic types of redis
SQL attendance query interval: one hour
PayPal账户遭大规模冻结!跨境卖家如何自救?
Rust async: SMOL source code analysis -executor
Redis configuration file details
100%弄明白5种IO模型
游戏资产复用:更快找到所需游戏资产的新方法
JVM层次上的对象的创建过程和内存布局
Filter filter
oracle用一条sql查出哪些数据不在某个表里
What are the specialties of database system engineers?
MySQL environment variable configuration tutorial