阻止事件冒泡:
1.e.stopPropagation()方法 IE浏览器不可以
2.cancelBbubble=true;属性 IE浏览器
代码如下:
wapper.onclick=function(event){
console.log('wapper');
//取消冒泡
stopBubble(event);
}
context.onclick=function(event){
console.log('context');
//取消冒泡
stopBubble(event);
}
//兼容浏览器
var event=e||windoow.event;
if(event.stopPropagation){
event.stopPropagation();
}else{//IE
event.cancelBubble=true;
}
}
阻止冒泡和阻止默认事件
1.event.stopPropagation()方法
阻止事件的冒泡方法,但是默认事件仍然会执行,如果点击一个连接,这个连接仍然会被打开,
2.event.preventDefault()方法
阻止默认事件的方法,连接不会被打开,但是会发生冒泡
3.return false
阻止事件冒泡也会阻止默认事件;连接不会被打开,事件也不会传递到上一层的父元素;等于同时调用了event.stopPropagation()和event.preventDefault()