⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙

代码-JS之阻止冒泡事件

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        #da{
            width:200px;
            height:200px;
            background-color: pink;
        }
        #xiao{
            width:100px;
            height:100px;
            background-color: purple;
        }
    </style>
</head>
<body>

<div id="da">
    <div id="xiao"></div>
</div>

<script>
    //定义兼容的阻止冒泡的函数
    function zuzhi(e){
        //标准浏览器使用   evt.stopPropagation(); //evt指的是事件对象
        //IE内核浏览器使用 window.event.cancelBubble = true; // window.event 事件对象
        if(e.stopPropagation){
            e.stopPropagation();
        }else{
            e.cancelBubble = true;
        }
    }
    //分别为da和xiao两个div绑定单击事件
    document.getElementById('da').onclick = function () {
        alert('da');
    };
    document.getElementById('xiao').onclick = function (evt) {
        alert('xiao');
        //调用函数阻止冒泡
        var e = window.event||evt;
        zuzhi(e);
    };
</script>

</body>
</html>
posted @ 2018-10-18 01:54  羊驼可以吃吗  阅读(196)  评论(0编辑  收藏  举报
⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙ ⊙︿⊙