点击页面其他地方隐藏弹窗

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script src="js/jquery-2.1.3.min.js"></script>
        <style>
            .tip{
                margin:300px auto;
                width:200px;
                height:100px;
                text-align: center;
                background:green;
                color:#fff;
                display:none;
            }
        </style>
    </head>
    <body>
        <button>点我</button>
            <div class="tip">
                这里是提示信息!提示信息!提示信息!
            </div>
        <script>
            $(function(){
                $('button').click(function(e){
                    $('.tip').show();
                    
                    e.stopPropagation();
                    $(document).one('click',function(){
                        $('.tip').hide();
                    })
                })
                
                $('.tip').click(function(e){
                    e.stopPropagation();
                })
            })
        </script>
    </body>
</html>

one() 方法为被选元素附加一个或多个事件处理程序,并规定当事件发生时运行的函数。

当使用 one() 方法时,每个元素只能运行一次事件处理器函数。

原文地址:https://www.cnblogs.com/wj19940520/p/6925208.html