当前位置:网站首页>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
边栏推荐
- c#的初步认识
- DataV轮播表组件dv-scroll-board宽度问题
- 期货反向跟单靠谱吗?
- [batch dos-cmd command - summary and summary] - map folder to virtual disk - subst
- Closure problem
- MSSQL how to export and delete multi table data using statements
- JS to print prime numbers between 1-100 and calculate the total number of optimized versions
- 【10. 差分】
- 游戏资产复用:更快找到所需游戏资产的新方法
- Set the address book function to database maintenance, and add user name and password
猜你喜欢

淘宝虚拟产品开店教程之作图篇

野风药业IPO被终止:曾拟募资5.4亿 实控人俞蘠曾进行P2P投资

一种太阳能电荷泵供电电路的方案设计

Reference | upgrade win11 mobile hotspot can not be opened or connected
![[13. number and bit operation of 1 in binary]](/img/53/024f9742d1936fe6f96f4676cea00e.png)
[13. number and bit operation of 1 in binary]

【批处理DOS-CMD命令-汇总和小结】-将文件夹映射成虚拟磁盘——subst

JVM层次上的对象的创建过程和内存布局
![[batch dos-cmd command - summary and summary] - output / display command - echo](/img/44/cddad70eeb756db15c19100c25c61a.png)
[batch dos-cmd command - summary and summary] - output / display command - echo

Blind survey shows that female code farmers are better than male code farmers

SPARQL基础入门练习
随机推荐
PayPal账户遭大规模冻结!跨境卖家如何自救?
(note) Anaconda navigator flashback solution
盲测调查显示女码农比男码农更优秀
University database mysql
洛谷刷题心得记录
What are the specialties of database system engineers?
(原创)自定义Drawable
Game asset reuse: a new way to find required game assets faster
After working in a large factory for ten years with an annual salary of 400000 yuan, I was suddenly laid off. If the company wanted to abandon you, it wouldn't leave any kindness
【12. 最大连续不重复子序列】
[batch dos-cmd command - summary and summary] - output / display command - echo
参考 | 升级 Win11 移动热点开不了或者开了连不上
The IPO of Yefeng pharmaceutical was terminated: Yu Feng, the actual controller who had planned to raise 540million yuan, made P2P investment
c#的初步认识
The 6th Blue Bridge Cup
Mysql-8 download, installation and configuration tutorial under Windows
Install Jenkins
SPARQL基础入门练习
Etcd教程 — 第五章 Etcd之etcdctl的使用
How to view program running time (timer) in JS