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

JS点击子元素不触发父元素点击事件(js阻止冒泡)

时间:2014-12-31 12:45:30      阅读:693      评论:0      收藏:0      [点我收藏+]

标签:

<html>
<title></title>
<head>
    <meta charset="utf-8">
    <style type="text/css">
        .divone{width:100px;height:100px;background:black;position: relative;cursor: pointer}
        .divchild{position: absolute;margin:10px;width:50px;height:50px;background:white;cursor: pointer}
    </style>
    <script type="text/javascript">
        function divone(){ 
            //这里是divone事件的代码 
            console.log(divone事件);
            stopPropagation();
        } 
        
        function divchild(){
            //这里是divchild事件的代码 
            console.log(divchild事件);
            stopPropagation();
        }

        function stopPropagation(e) {  
            e = e || window.event;  
            if(e.stopPropagation) { //W3C阻止冒泡方法  
                e.stopPropagation();  
            } else {  
                e.cancelBubble = true; //IE阻止冒泡方法  
            }  
        }
    </script>
</head>
<body>
    
    <div class="divone" onclick="divone(this)"> 
        
        <div class="divchild" onclick="divchild(this)"></div> 
        
    </div> 
 
</body>
</html>

 

JS点击子元素不触发父元素点击事件(js阻止冒泡)

标签:

原文地址:http://www.cnblogs.com/cnsevennight/p/4195279.html

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