js事件中有时候需要阻止浏览器的默认行为以及阻止事件冒泡来避免对定义事件产生影响,阻止默认行为写法为
function stopDefault(e) {
if (e && e.preventDefault) {
e.preventDefault();//防止浏览器默认行为(W3C)
} else {
window.event.returnValue = false;//IE中阻止浏览器行为
}
return false;
}
阻止事件冒泡的写法为
function stopPropagation(event) {
if (event.stopPropagation) {
event.stopPropagation();
} else {
event.cancelBubble = true;
}
}
阻止事件捕获,网上找了一下感觉没有什么好的写法,这里推荐一种写法是给子元素定义阻止冒泡事件实现事件不向下传递