码迷,mamicode.com
首页 > Web开发 > 详细

js中阻止事件冒泡

时间:2015-07-03 12:37:12      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:

<html>
        <head>
                <title>js阻止时间冒泡</title>
        </head>
        <style>
                #bubble{width:300px;height:300px;background:red;}
                #bubble2{width:200px;height:200px;background:green;}
                #bubble3{width:100px;height:100px;background:blue;}
        </style>
        <body>
                <div id="bubble" onclick="bubble(this, event)">
                <div id="bubble2" onclick="bubble(this, event)">
                        <div id="bubble3" onclick="bubble(this, event)">this one</div>
                                this two
                        </div>
                        this three
                </div>
        <body>
</html>
<script>
    //阻止事件冒泡
    function stopEventBubble(event){
        var e=event || window.event;
        if (e && e.stopPropagation){
            e.stopPropagation();
        }
        else{
            e.cancelBubble=true;
        }
    }
    
    function bubble(obj, evt){
        alert(obj.id);
        stopEventBubble(evt);
    }
</script>

js中阻止事件冒泡

标签:

原文地址:http://my.oschina.net/tpythoner/blog/473925

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!