Skip to content

事件对象

事件冒泡

事件冒泡是事件被定义为从最具体的元素开始触发,然后向上传播值没有那么具体的元素。

阻止冒泡的方法

IE方法: 其他浏览器也生效

		 window.event.cancelBubble=true

document方法:IE8以前不生效

​ event.stopPropagation()

js
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #box {
            width: 100px;
            height: 100px;
            background-color: aquamarine;
        }
    </style>
    <script>
        window.onload = function() {
            var odiv = document.getElementById('box');
            odiv.onclick = function(e) {
                alert('odiv');
                var e = window.event || e
                    // e.stopPropagation();
                e.cancelBubble = true;
            }
            document.onclick = function(e) {
                alert('document');
            }
        }
    </script>
</head>

<body>
    <div id="box">
    </div>
</body>

</html>

==注意:==blur,focus,load,unload不会事件冒泡