当前位置:网站首页>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
})
边栏推荐
猜你喜欢
随机推荐
General Lei's personal blog to see
联想笔记本 如何更改Win10系统开机logo图标
Burpsuite几种爆破方式
typescript6 - simplify the steps to run ts
DP5340 domestic replacement for CM5340 stereo audio A/D converter chip
潜心打磨,主动求变——这群技术排头兵,如何做好底层开发这件事?
2022年施工企业数字化转型思考,施工企业数字化转型之路
用代码收集每天热点内容信息,并发送到自己的邮箱
Max Sum Plus Plus HDU - 1024
Link with Bracket Sequence II(杭电多校赛)
The blockbuster IP that has been popular in the world for 25 years, the new work has become a script paradise
typescript4 - installs a toolkit for compiling ts
风靡全球25年的重磅IP,新作沦为脚本乐园
ES:模板字符串的使用
Portable small fan PD power chip
golang grpc protoc 环境配置
Lenovo Notebook How to Change Windows 10 Boot Logo Icon
mysql URL链接
Hands-on teaching OneOS FOTA upgrade
muduo库学习记录(一)