Skip to content

JS解惑-如何阻止事件冒泡和取消默认行为 #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
sunmaobin opened this issue Nov 19, 2017 · 0 comments
Open

JS解惑-如何阻止事件冒泡和取消默认行为 #37

sunmaobin opened this issue Nov 19, 2017 · 0 comments
Assignees
Milestone

Comments

@sunmaobin
Copy link
Owner

sunmaobin commented Nov 19, 2017

JS解惑-如何阻止事件冒泡和取消默认行为

什么是事件冒泡?

要了解事件冒泡,我们就得把它跟事件捕获来一起交代!

一组栗子

<div id="d1">
    <p id="p1">
        <span id="s1">
            Click Here
        </span>
    </p>
</div>

加入我给如上HTML的三级标签上都加上Click事件:

//注意这里的最后一个参数,默认也是false
document.getElementById('d1').addEventListener('click',function(e){
    console.log('d1');   
},false);

document.getElementById('p1').addEventListener('click',function(e){
    console.log('p1');   
},false);

document.getElementById('s1').addEventListener('click',function(e){
    console.log('s1');   
},false);

当点击页面上的 Click Here 的文字时,控制台会依次打印:

s1
p1
d1

也就是这3个事件的触发是由内层标签开始朝外依次进行的。

如果这时候修改一下事件绑定的方式,如下:

//最后一个参数修改为true
document.getElementById('d1').addEventListener('click',function(e){
    console.log('d1');   
},true);

document.getElementById('p1').addEventListener('click',function(e){
    console.log('p1');   
},true);

document.getElementById('s1').addEventListener('click',function(e){
    console.log('s1');   
},true);

再次点击页面上的 Click Here 的文字时,控制台会依次打印:

d1
p1
s1

原因解释

如何阻止事件冒泡

先说明下,IE旧的浏览器对于事件的定义不同(<=IE8),所以当你使用旧浏览器的时候,务必考虑兼容性!

function stopPropagation(e){
    if(e && e.stopPropagation){//支持W3C的stopPropagation()方法
        e.stopPropagation();
        return;
    };

    if(window.event){//<=IE8,取消事件冒泡
        window.event.cancelBubble = true;
    };
}

什么是默认行为?

看一些栗子:

  • <a> 标签,我们点击以后默认会跳转到 href属性配置的地址去,这就是 <a> 的默认行为;
  • <input type="submit"> 如果放在 <form> 标签中,点击后默认会提交该 <form> 表单,这就是 <input type="submit"> 的默认行为;

如何阻止默认行为?

function stopDefault(e){
    if(e && e.preventDefault){//支持W3C的preventDefault()方法
        e.preventDefault();
        return;
    };

    if(window.event){//<=IE8,阻止默认行为
        window.event.returnValue = true;
    };
}

总结

支持W3C标准

  • 阻止冒泡:e.stopPropagation
  • 阻止默认行为:e.preventDefault

IE8及以下非W3C标准

  • 阻止冒泡:window.event.cancelBubble = true
  • 阻止默认行为:window.event.returnValue = true

补充

使用jQuery的事件方法,直接使用 return false 既可以阻止冒泡也可以阻止默认行为

$(ele).click(function(){
    //...

    //阻止冒泡和默认行为
    return false;
});

(全文完)

@sunmaobin sunmaobin added this to the 2017年 milestone Nov 19, 2017
@sunmaobin sunmaobin self-assigned this Nov 19, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant