当前位置:网站首页>day32 js笔记 事件(上)2021.09.27
day32 js笔记 事件(上)2021.09.27
2022-06-28 11:20:00 【即将成为大佬的小梦】
事件三要素:事件绑定、事件类型、事件函数
常见的处理:
1.在标签或者js文件中 dom元素.事件 = function(){}
2.function fn(){} dom元素.onclick = fn
- 这种不要在函数名称后面添加小括号 自己调用执行
注意:提交表单的时候不要给form表单绑定单击事件,表单会默认提交 提交后就相当于刷新页面跳转到新的页面 表单中submit方法
事件流:
- 捕获阶段:从代码的最外层开始往代码的内层去查找,是一个查找绑定事件的过程(从外层到内层)
- 目标阶段:到达目标事件的位置 触发事件
- 冒泡阶段:从目标事件开始又往html跟文档方向再次回溯(从内层到外层) 浏览器默认事件!!
事件绑定: 之前使用事件绑定的方法是 on+事件类型 这种方式可以但在特殊情况下是有bug的
- 同类型的事件 绑定在一个标签上 就会出现bug 后面的会把前面的覆盖
监听器/侦听器
语法: dom元素.addEventListener('不加on的事件类型',事件处理函数function(){
// 执行的代码段
},是否在捕获阶段执行-常用的是false默认值是在冒泡阶段)

重点:ele.addEventListener(type, handler) 写法要记住 可以解决on+事件类型的bug
监听器绑定:

监听器解绑:

事件对象:

阻止事件冒泡:
--目的:如果有多个盒子嵌套 点击最小盒子进行跳转 由于冒泡的存在可能会影响到当前页面的打开跳转 会打开最外面的这个页面进行跳转解析 所以这种情况是有不符合我的需求的 阻止默认的冒泡行为
阻止冒泡: 事件对象
- stopPropagation() 是一个方法 (停止传播) 只能在高版本中使用
- e.cancelBubble = true 在低版本中使用

阻止浏览器默认行为的方法:preventDefault()
边栏推荐
- [sword finger offer] 49 Ugly number
- Word、PDF、TXT文件实现全文内容检索需要用什么方法?
- Gee: mcd64a1 based globfire daily fire data set
- How to distinguish and define DQL, DML, DDL and DCL in SQL
- mysql-. SQL file phishing Online
- [monkey] Introduction to monkey test
- 合约量化交易系统开发 | 合约量化APP开发(现成案例)
- Making and using of dynamic library (shared library)
- 什么是主链系统?
- 动态库(共享库)的制作和使用
猜你喜欢

如临现场的视觉感染力,NBA决赛直播还能这样看?

字符串 & 堆 & 方法区

Wealth management for programmers

Training notice | special training notice on epidemic prevention and security prevention for overseas Chinese funded enterprises, institutions and personnel in 2022

数据库系列:有什么办法对数据库的业务表进行无缝升级

Characteristics of solar wireless LED display

JS基础3

一套十万级TPS的IM综合消息系统的架构实践与思考

Industry analysis - quick intercom, building intercom

TiDB v6.0.0 (DMR) :缓存表初试丨TiDB Book Rush
随机推荐
董宇辉,新东方以及凤凰卫视
毕业季,给初入社会的你一些建议
Solve the problem of reading package listsdonebuilding dependency treereading state informationdone
[semidrive source code analysis] [x9 chip startup process] 32 - play module analysis - RTOS side
分析list中有无重复数据且重复了几次
JS基础2
JS基础8
Everyone can participate in open source! Here comes the most important developer activity in dragon lizard community
Characteristics of solar wireless LED display
QML控件类型:TabBar
Convert the file URL in the browser to a file stream
JS基础5
GCC introduction
What is the function of ICMP Protocol and the principle of Ping of death attack?
实体转JSON时,值为null的字段的丢失问题
MySQL installation configuration and solving the problem of forgetting root password when reinstalling MySQL
Makefile简介
合约量化交易系统开发 | 合约量化APP开发(现成案例)
行业分析| 快对讲,楼宇对讲
Oracle 日期格式化异常:无效数字