js-事件1_阻止事件冒泡_弹框菜单

原文链接:http://www.cnblogs.com/XChWaad/p/4910151.html

点击按钮的时候,弹出菜单。点击其他地方的时候,隐藏菜单。代码如下:

 

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
 <meta charset="utf-8" />
    <style>
        div {
        width:100px;
        height:150px; background:#ccc;display:none;
        }
    </style>
    <script>
        window.onload = function () {
            var oBtn = document.getElementById("btn1");
            var oDiv = document.getElementById("div1");
            oBtn.onclick = function (ev) {
                var oEvent = ev || event;
                oDiv.style.display = "block";
                ev.cancelBubble = true;
            };
            document.onclick = function () {
                oDiv.style.display = "none";
            }
        }
    </script>
</head>
<body>
    <button id="btn1">显示</button>
    <div id="div1"></div>
</body>
</html>

 

转载于:https://www.cnblogs.com/XChWaad/p/4910151.html

上一篇:python读取日志,存入mysql的代码教程


下一篇:德州扑克学习笔记20190801