当前位置:网站首页>事件
事件
2022-06-24 03:54:00 【InfoQ】
事件三要素
//举例
div.onclick = function () {};
div: 事件源(绑定在 div 身上的事件);
click: 事件类型;
function () {}: 事件处理函数, 当行为发生的时候, 执行这个函数; 事件绑定分类
//CSS样式部分
<style>
div {
width: 200px;
height: 200px;
background-color: pink;
}
</style> //dom0级事件
div.onclick = function ()
{ console.log('我被点击了');
}
// 2-1. addEventListener()
div.addEventListener('click', function () {
console.log('你好 世界')
})
// 2-2. attachEvent()
div.attachEvent('onclick', function () {
console.log('你好 世界')
})事件的解绑
1. 解绑 dom0级 事件
2. 解绑 dom2级 事件
3.绑定事件
div.addEventListener('click', function b() { console.log('hello world') })
btn.onclick = function () {
// 给 div 解绑 dom2级 事件
// 在解绑的时候, 你传递进去了一个 叫做 a 的函数
// 就要找到 div 身上有没有绑定一个叫做 a 的函数, 发现没有, 解绑不掉
div.removeEventListener('click', function a() { console.log('hello world') })
console.log('执行完毕解绑代码')
}
封装事件绑定和解绑
开始封装
1. 参数验证
1-1. ele 必传, 如果没有传递, 直接报错
1-2. ele 需要是一个 元素节点
节点类型, nodeType: 1
2. 兼容处理
2-1. if () {} else {} //兼容处理
div.addEventListener()
div.attachEvent()
//只要 元素 身上有这个函数, 就可以执行
// 没有这个函数就去换另一个函数试试
if (ele.addEventListener) {
ele.addEventListener(type, handler)
} else if (ele.attachEvent) {
ele.attachEvent('on' + type, handler)
} else {
//对象操作语法 - 点语法
ele.onclick = handler
//对象操作语法 - 数组关联语法
ele['onclick'] = handler
ele['on' + type] = handler
}
}事件对象
如何获取事件对象
// 0. 获取元素
var div = document.getElementById('box')
// 1. 绑定事件
div.onclick = function (e) {
console.log(e)
console.log(window.event)
e = e || window.event
console.log(e)
}
div.addEventListener('click', function (e) {
console.log(e)
})
on(div, 'click', function (e) {
// 处理事件对象兼容
e = e || window.event
console.log(e)
})边栏推荐
- Tell you about mvcc
- Troubleshoot the high memory consumption of Go program
- C language - number of bytes occupied by structure
- Clang code coverage detection (pile insertion technology)
- Openeuler kernel technology sharing issue 20 - execution entity creation and switching
- Worthington胰蛋白酶的物化性质及特异性
- Leetcode refers to offer II 089 House theft
- How to select a telemedicine program system? These four points are the key!
- Gpt/gpt2/dialogpt detailed explanation comparison and application - text generation and dialogue
- 共建欧拉社区 共享欧拉生态|携手麒麟软件 共创数智未来
猜你喜欢

祝贺钟君成为 CHAOSS Metric Model 工作组的 Maintainer

Training course of mixed accuracy from simple to deep

Common content of pine script script

How can the new generation of HTAP databases be reshaped in the cloud? Tidb V6 online conference will be announced soon!

Clang代码覆盖率检测(插桩技术)

What is etcd and its application scenarios

Multi task video recommendation scheme, baidu engineers' actual combat experience sharing

How does the compiler put the first instruction executed by the chip at the start address of the chip?

英特尔 XTU 官方超频工具已支持 Win11 22H2 和 13 代酷睿 Raptor Lake 处理器

The official overclocking tool of Intel XTU supports win11 22h2 and 13th generation core Raptor Lake processors
随机推荐
抢先报名丨新一代 HTAP 数据库如何在云上重塑?TiDB V6 线上发布会即将揭晓!
Unable to access the CVM self built container outside the TKE cluster pod
Download files and close the enhanced module security configuration to visit the website for the first time using IE browser
Two most practical methods for cadence OrCAD capture to batch modify network names graphic tutorial and video demonstration
Kubernetes resource topology aware scheduling optimization
What is pseudo static? How to configure the pseudo static server?
Multi task video recommendation scheme, baidu engineers' actual combat experience sharing
Advanced authentication of uni app [Day12]
Several good books for learning data
An open source monitoring data collector that can monitor everything
Clickhouse synchronous asynchronous executor
Diskpart San policy is not onlineall, which affects automatic disk hanging
From virtual to real, digital technology makes rural funds "live"
There is such a shortcut to learn a programming language systematically
Troubleshoot the high memory consumption of Go program
Changjiang Dayong, director of openeuler community: jointly promote the new open source model of Euler and jointly build a new open source system
Web technology sharing | [map] to realize customized track playback
Congratulations to Zhong Jun for becoming the maintainer of chaos metric model working group
What is a 1U server? What industries can 1U servers be used in?
How to adjust the incompleteness before and after TS slicing of easydss video recording?