当前位置:网站首页>JS的DOM操作——事件对象
JS的DOM操作——事件对象
2022-07-19 00:42:00 【M78_国产007】
事件触发时,handler函数内部会传入数据,这个数据就是事件对象。
事件的绑定方式有三种,行内绑定、属性绑定、监听器绑定。
其中是行内绑定时,行内无法获取事件对象;属性绑定和监听器绑定时,事件对象在参数中。
那么如何解决行内无法获取事件呢?
我们采用兼容写法,不管是那种方法触发的,我们都先判断判断一下参数e,
e=(e.constructor==PointerEvent&&e)||window.event//兼容写法
代码展示:
<div class=".test" onclick="fn1()">
111
</div>
<script>
function fn1(e){
e=(e.constructor==PointerEvent&&e)||window.event//兼容写法
console.log(e);
}
</script>
打印结果:
展示一下属性绑定和监听器绑定时获取事件对象:
<div class="boxx">111</div>
<script>
var boxx=document.querySelector(".boxx")
boxx.onclick=function(e){
console.log(e);
}
boxx.addEventListener("click",function(e){
console.log(e);
})
</script>
运行结果可见,事件触发时,产生的数据会传入处理函数,这个数据正是我们需要的事件对象。
我们还需了解事件中的this:
在行内绑定中:函数的调用者为window
在属性绑定中:this指向的是dom元素本身
在监听器绑定中:this指向的是dom元素本身
边栏推荐
- 【Jailhouse 文章】Specific Electronic Platform to Test the Influence of Hypervisors on the Performance..
- The most basic naming standard is also the basic quality that can best reflect whether a programmer is a good programmer
- 微服务高可用的2个关键技巧,你一定用得上!
- QQ group cannot download video and picture solutions
- 广发证券怎么样?网上开户安全吗?
- 键入网址到网页显示,期间发生了什么?
- My creation anniversary
- &lt;/script&gt;&lt;script&gt;console.log(7890)-{&quot; xxx&quot; :&quot; aaa
- 【Web3 系列开发教程——创建你的第一个 NFT(3)】开始创建 NFT
- STM32状态机编程实例——全自动洗衣机(上)
猜你喜欢
Financial banking software testing super large strategy, the most popular financial banking big secret cover questions
【体系结构 四 存储结构】
EN 1504-2 concrete structure protection and repair products - CE certification
Flutter启动流程(Isolate机制+创建流程+线程练习)
高数 | 【多元函数微分学】概念篇 —— 连续、可偏导及可微之间的关系
【牛客刷题】/*日常四道编程小题分享*/
INE Penetration Testing Basics 黑盒渗透测试过程
自定义MVC增删改查
高数 | 多元函数求极限 使用极坐标代换的条件与细节
Tutoriel de requête SQL pour la science des données avec mon serveur SQL
随机推荐
ovirt: api
FAST-LIO,FAST-LIO2与FASTER-LIO
3年软件测试经验,薪资一直卡在10k,自动化测试如何提升和发展?
【微信小程序】常用组件及基本使用详解
6、【WebGIS实战】软件操作篇——WebGIS开发环境搭建
Erreur de modification du mot de passe MySQL 1064 (42000):
[surveying] leveling
NahamCon CTF 2022 Babyrev逆向分析
ovirt: api
MySQL系列二:CRUD
The most basic naming standard is also the basic quality that can best reflect whether a programmer is a good programmer
使用 My SQL Server 实现数据科学的 SQL 查询教程
靶机无法获得IP地址, 该怎么办?
Leetcode-394-string decoding
10天学会STM32的学习心得总结
MySQL string operation (substr interception, locate instr position child parent string judgment)
View的measure测量过程梳理
6. [WebGIS practice] software operation -- WebGIS development environment construction
金融银行软件测试超大型攻略,最受欢迎的金融银行大揭 秘附面试题
中间抽头的变化对于ZVS振荡电路的影响