1. 程式人生 > >原聲js,取消事件冒泡,點擊按鈕,顯示box,點擊屏幕其他地方,box隱藏

原聲js,取消事件冒泡,點擊按鈕,顯示box,點擊屏幕其他地方,box隱藏

lang || width pre document nodes onclick 谷歌 one

<!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="width:500px;height: 300px;background: gray; display: none"></div> </body> </html>

原聲js,取消事件冒泡,點擊按鈕,顯示box,點擊屏幕其他地方,box隱藏