冒泡
Google是e.stopPropagation(),IE则是使用e.cancelBubble = true
jquery
// return false 方式一
$('.son').click(()=>{
alert('我是son的事件');
return false;
})
// event.stopPropagation 方式二
$('.son').click((event)=>{
alert('我是son的事件');
event.stopPropagation()
})
兼容
//兼容一
window.event? window.event.cancelBubble = true : e.stopPropagation();
//兼容二
function stopBubble(e) {
//如果提供了事件对象,则这是一个非IE浏览器
if ( e && e.stopPropagation )
//支持stopPropagation()方法
e.stopPropagation();
else
//否则,我们需要使用IE的方式来取消事件冒泡
window.event.cancelBubble = true;
}
默认行为
Google是e.preventDefault(),IE则是使用e.returnValue = false;
//阻止默认行为方式一 return false
$('a').click(()=>{
alert('填写注册框')
return false;
})
//阻止默认行为方式二 event.preventDefault()
$('a').click((event)=>{
alert('填写注册框')
event.preventDefault()
})
兼容
//阻止浏览器的默认行为
function stopDefault( e ) {
//阻止默认浏览器动作(W3C)
if ( e && e.preventDefault )
e.preventDefault();
//IE中阻止函数器默认动作的方式
else
window.event.returnValue = false;
return false;
}
事件兼容问题
在IE/Opera中是window.event,在Firefox中是event;
而事件的对象,在IE中是window.event.srcElement,在Firefox中是event.target,Opera中两者都可用。