当前位置:网站首页>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()
边栏推荐
- 爱可可AI前沿推介(6.28)
- Yann LeCun新论文:构建自动智能体之路
- Basic 02: variable, remember the mobile number of the object
- windows 10下载安装mysql5.7
- Spatial-Temporal时间序列预测建模方法汇总
- 2022 open source software security status report: over 41% of enterprises do not have enough confidence in open source security
- ProCAST有限元铸造工艺模拟软件
- JS基础3
- Using loops for, while, and if in katalon else、break、continue
- 2022 开源软件安全状况报告:超41%的企业对开源安全没有足够的信心
猜你喜欢

静态库的制作和使用

New listing of operation light 3.0 - a sincere work of self subversion across the times!

Wealth management for programmers

Scientific research - web of science retrieval skills

Making and using of static library

获取系统当前日期

Yann LeCun新论文:构建自动智能体之路

还在用 SimpleDateFormat 做时间格式化?小心项目崩掉!

Get current system date

day36 js笔记 ECMA6语法 2021.10.09
随机推荐
Gee: mcd64a1 based globfire daily fire data set
网页提示此站点不安全解决方案
Create ECS using API shortcut
培训通知|2022年境外中资企业机构及人员疫情防控和安全防范专题培训通知
个人买场内基金选择什么证券公司开户好,更安全
使用API快捷创建ECS
[function suggestion] select a space when multiple workspaces are started
字符串 & 堆 & 方法区
Word、PDF、TXT文件实现全文内容检索需要用什么方法?
Redis6 一:Nosql引入、Redis可以解决什么问题?
2022 open source software security status report: over 41% of enterprises do not have enough confidence in open source security
MySQL cannot query the maximum value using the max function
[QT] connect syntax reference implementation
Metersphere implements UI automation elements that are not clickable (partially occluded)
Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system
Download and install mysql5.7 for windows 10
[semidrive source code analysis] [x9 chip startup process] 32 - play module analysis - RTOS side
【sciter】: sciter-fs模块扫描文件API的使用及其注意细节
还在用 SimpleDateFormat 做时间格式化?小心项目崩掉!
【SemiDrive源码分析】【X9芯片启动流程】32 - DisPlay模块分析 - RTOS侧