当前位置:网站首页>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
})
边栏推荐
猜你喜欢
随机推荐
数据库连接池的使用
svn中文路径 权限设定
2020 ACM | MoFlow: An Invertible Flow Model for Generating Molecular Graphs
实现定时器
SkiaSharp 之 WPF 自绘 拖曳小球(案例版)
Judging from the Internet:
OA项目之待开会议&历史会议&所有会议
RFID固定资产盘点系统给企业带来哪些便利?
ES: 箭头函数和包裹它的代码共享相同的this
The blockbuster IP that has been popular in the world for 25 years, the new work has become a script paradise
获取controller中所有接口路径和名称
C language custom types, rounding
Common configuration
MagicDraw secondary development process
The difference between typescript3-ts and js
File类
ARM体系结构概述
selenium模块
42.【vector简单列题】
如何实时计算日累计逐单资金流








