当前位置:网站首页>事件对象学习
事件对象学习
2022-07-21 01:37:00 【抱抱旋旋子】
一.什么是事件对象
官方解释: event对象代表事件的状态,比如键盘按键的状态、鼠标的位置、鼠标按钮的状态。
简单理解∶事件发生后,跟事件相关的一系列信息数据的集合都放到这个对象里面,这个对象就是事件对象event,它有很多属性和方法。
事件对象兼容性方案
事件对象本身的获取存在兼容问题:
1.标准浏览器中是浏览器给方法传递的参数,只需要定义形参e就可以获取到。
2.在IE6~8中,浏览器不会给方法传递参数,如果需要的话,需要到window.event中获取查找。
解决:
e = e ll window.event;
二.事件对象的常用属性和方法
常见事件对象的属性和方法
e.target和this的区别
e.target返回的是触发事件的对象(元素)
this返回的是绑定事件的对象(元素)
区别:e.target点击了那个元素,就返回那个元素 this,那个元素绑定了这个点击事件,那么就返回谁
了解兼容性问题
三.阻止默认行为
dom标准写法 :e.preventDefault();
低版本浏览器ie678 :returnValue属性
四.阻止冒泡
事件冒泡∶开始时由最具体的元素接收,然后逐级向上传播到到DOM最顶层节点。
阻止冒泡的两种方式
1.标准写法:利用事件对象里面的stopPropagation()方法 (stop停止Propagation传播)
2.非标准写法:IE6-8利用事件对象cancelBubble属性
e.cancelBubble = true (非标准cancel取消 bubble 泡泡)
阻止事件冒泡的兼容性解决方案
例子:
五.事件委托
事件委托也称为事件代理,在jQuery里面称为事件委派。
原理:
不是每个子节点单独设置事件监听器,而是事件监听器设置在其父节点上,然后利用冒泡原理影响设置每个子节点。
以上案例:给ul注册点击事件,然后利用事件对象的target 来找到当前点击的li,因为点击li,事件会冒泡到u上,u有注册事件,就会触发事件监听器。
作用:
我们只操作了一次DOM,提高了程序的性能。
例子:
边栏推荐
- JS SVG cloud and love animation JS special effects
- npm 设置下载源
- CLWY权限管理(六)--- 权限实现
- Deep tide finance interprets usdd: look for a real stable currency through the illusion of decoupling
- NFS FTP PXE
- 不知道 MySQL 咋学?刷完牛客这 50 道题就够了!(第八篇)
- pytorch-California House Prices
- Answer: intelligent cockpit series article 1, what is it
- CAD browsing mode and drawing mode, how CAD prints hundreds of drawings at one time
- LabelImg(目标检测标注工具)的安装与使用教程
猜你喜欢
"In order to buy a mobile phone, I still have no choice after half a month of research."
DNS域名解析服务
Network simulators ENSP, Eve ng, GNS3, packet tracert
NPM settings download source
UneXt 基于MLP的快速医学图像分割网络
The network cannot be found in the network icon in the lower right corner of the win10 desktop
Nonext fast medical image segmentation network based on MLP
Using zdog to realize JS special effect of outlook lighthouse animation
[C]语言动态内存管理
PHP-CGI远程代码执行漏洞(CVE-2012-1823)
随机推荐
Don't know how to learn MySQL? It's enough to finish the 50 questions of Niuke! (Part VIII)
数据库表设计(二):索引规范和SQL语句规范
2D array theme
Don't know how to learn MySQL? It's enough to finish the 50 questions of Niuke! (Part VII)
不知道 MySQL 咋学?刷完牛客这 50 道题就够了!(第七篇)
Rxjs see the moon JS special effect code through the window
VII Practice -- some methods of common tools
JSON定义的语法 [JSON]
如何从多重插补的数据库中提取指定列作为新变量
CLWY权限管理(四)--- 菜单与权限模块
npm 设置下载源
MFC calculator
IBM3650M4实体机安装ESXI7.0
IDEA 忽略target目录
pytorch-California House Prices
UneXt 基于MLP的快速医学图像分割网络
The network cannot be found in the network icon in the lower right corner of the win10 desktop
JS SVG cloud and love animation JS special effects
Find the maximum of 10 integers
Appium automated testing