原声js,取消事件冒泡,点击按钮,显示box,点击屏幕其他地方,box隐藏

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>js 事件event</title>
    <script>
        window.onload=function() {
            var button=document.getElementById("button");
            var box=document.getElementById("box");
            button.onclick=function(ev){
                 box.style.display="block";
                 var oEvent=ev||event;     //兼容火狐,ie,谷歌
                 oEvent.cancelBubble=true; //取消事件冒泡
            }

            document.onclick=function(ev){
                box.style.display="none";
                
                //var oEvent=ev||event;
                //alert(oEvent.clientX+","+ oEvent.clientY);//兼容火狐,ie,谷歌
                

            }
            // var ss = document.childNodes[1].tagName;
            // alert(ss);  //html
        }
    </script>
</head>
<body>

    <input type="button" value="按钮" id="button">
    <div id="box" style="500px;height: 300px;background: gray; display: none"></div>
</body>
</html>
原文地址:https://www.cnblogs.com/quitpoison/p/9900648.html