阻止冒泡的方法 event.stopPropagation();
来源:互联网 发布:115浏览器for mac 编辑:程序博客网 时间:2024/05/20 14:18
标准浏览器 和 ie浏览器
w3c的方法是event.stopPropagation()
IE则是使用event.cancelBubble = true bubble 冒泡 泡泡 cancel 取消
兼容的写法:
if(event && event.stopPropagation)
{
event.stopPropagation(); // w3c 标准
}
else
{
例子二
w3c的方法是event.stopPropagation()
IE则是使用event.cancelBubble = true bubble 冒泡 泡泡 cancel 取消
兼容的写法:
if(event && event.stopPropagation)
{
event.stopPropagation(); // w3c 标准
}
else
{
event.cancelBubble = true; // ie 678 ie浏览器8
}
例子一
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><body><button id="btn">按钮</button></body><script> var btn=document.getElementsByTagName("button")[0]; document.onclick=function(){ alert("点击空白"); } btn.onclick=function(event){ alert("点击button"); //阻止冒泡 event.stopPropagation(); } window.onclick=function(){ alert("点击window"); }</script></html>
例子二
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <style type="text/css"> *{ margin: 0px; padding: 0px; border: 0px; } #bg{ width: 100%; height:100%; opacity: 0.5; background-color: #333; position: fixed; top:0; display: none; } #show{ width: 300px; height: 300px; background-color: white; position: relative; top:20%; left: 40%; display: none; } </style></head><body><a href="#">登录</a><a href="#">注册</a><div id="bg"></div><div id="show"></div></body><script> var mya=document.getElementsByTagName("a")[0]; var bg=document.getElementById("bg"); var show=document.getElementById("show"); mya.onclick = function(event){ bg.style.display="block"; show.style.display="block"; event.stopPropagation(); } document.onclick=function(event){ var target=event.target.id; alert(target); if(target!="show"){ bg.style.display="none"; show.style.display="none"; } }</script></html>
}
0 0
- 阻止冒泡的方法 event.stopPropagation();
- 阻止事件冒泡event.stopPropagation()
- AngularJS阻止事件冒泡$event.stopPropagation()
- 阻止事件冒泡,阻止默认事件,event.stopPropagation()和event.preventDefault(),return fal的区别
- s事件之event.preventDefault()与event.stopPropagation()的阻止默认事件和阻止事件冒泡的用法
- 阻止事件冒泡stopPropagation的兼容写法
- e.stopPropagation()阻止冒泡
- jQuery中event.stopPropagation()阻止事件冒泡及event.preventDefault()阻止默认行为
- event.stopPropagation()方法的使用
- jquery的冒泡事件event.stopPropagation()
- jquery的冒泡事件event.stopPropagation()兼容问题
- jquery阻止冒泡事件:$('span').bind("click",function(event){event.stopPropagation();})(有用源)
- jquery阻止冒泡事件:$('span').bind("click",function(event){event.stopPropagation();})(有用源)
- 阻止默认和冒泡事件,认清event.preventDefault()、event.stopPropagation()和return false区别
- stopPropagation()方法阻止事件的向上传播?
- 阻止js事件冒泡的例子(cancelBubble 、stopPropagation)
- Event的stopImmediatePropagation和stopPropagation方法
- 事件冒泡 ,阻止事件冒泡 e.stopPropagation()
- (转)Linux NTP配置详解 (Network Time Protocol)
- 绝句 自省
- 面向对象设计的八个原则
- 总结一下Android中主题(Theme)的正确玩法
- IDEA闪退问题
- 阻止冒泡的方法 event.stopPropagation();
- 最长上升子序列(nlogn算法)
- 视频会议所需带宽
- java基础学习之this,类变量,三大特征
- hdu5834 Magic boy Bi Luo with his excited tree(树形dp)
- IDEA闪退问题
- CodeForces 688B Lovely Palindromes(偶数位回文数)
- WebStorm开发工具设置React Native智能提示
- 思维 hdu 5512(Pagodas)