当前位置:网站首页>JS中如何阻止事件冒泡和默认行为
JS中如何阻止事件冒泡和默认行为
2022-07-30 07:15:00 【weixin_46051260】
事件对象:event对象,当成形参来看,包含所有与事件有关的信息,比如触发事件的元素,事件的类型。。。
阻止默认行为
e.preventDefault()—>有兼容性
e.returnValue=false—>ie 678也可用
return false—》没有兼容性问题,只限于传统方式
a.addEventListener('click',function(e){
// e.preventDefault()
e.returnValue=false
})
a.onclick=function(e){
return false
}
阻止事件冒泡
e.stopPropagation()----》兼容性
e.cancelBubble=true—》ie 678也可用
var one =document.querySelector('.one')
one.addEventListener('click',function(e){
console.log(e);
alert('我是子元素')
// e.stopPropagation()
e.cancelBubble=true
})
边栏推荐
- 电脑文档误删除怎么恢复,恢复误删除电脑文档的方法
- Limit injection record of mysql injection in No. 5 dark area shooting range
- IDEA搜索插件无结果一直转圈圈的解决办法
- Mybitatis related configuration files
- LeetCode:647. 回文子串
- WinForm(一):开始一个WinForm程序
- 解决datagrip连接sqlserver报错:[08S01] 驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接。
- 2022牛客暑期多校训练营3(ACFGJ)
- typescript6 - simplify the steps to run ts
- AutoSAR EcuM系列01- EcuM模块的功能概述和变体类型
猜你喜欢
随机推荐
风险登记册
一文带你玩转offer-01
防止资源导出失败
动态规划专栏
SQL injection vulnerability (postgresql injection)
Interview with Ant: How do these technology pioneers do the bottom-level development well?| Excellent technical team interview
Mybitatis related configuration files
电脑文档误删除怎么恢复,恢复误删除电脑文档的方法
SQL行列转换
C# uses RestSharp to implement Get, Post requests (2)
C语言自定义类型详解
c语言变量的存储方式和生存期 -考察
MongoDB - 千万级数据脚本过滤笔记
AutoSAR EcuM系列02- Fixed EcuM的状态管理
解决datagrip连接sqlserver报错:[08S01] 驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接。
The difference between typescript3-ts and js
linux安装mysql8参考指引
typescript7-typescript common types
ES:模板字符串的使用
selenium module









