點擊隱藏顯示和點擊body空白處隱藏


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

<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
        *{margin:0; padding:0;}
        .btn{background:#BF8B8B; width: 150px; height: 30px; line-height: 30px; margin: 0 auto; text-align: center; border-radius: 10px; display: block; text-decoration: none; color: #fff; font-size: 16px;}
        .pop{ width: 200px; height: 150px; margin: 20px auto; background: red; display: none;  }
    </style>
</head>

<body>
    <a href="javascript:;" class="btn">點擊</a>
    <div class="pop">彈窗顯示</div>
    <script src="http://apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    $(function() {
        $(".btn").click(function(event) {
            if ($(".pop").is(":hidden")) {
                $(".pop").show();
            } else {
                $(".pop").hide();
            }
            event.stopPropagation();
        });
        $('body').click(function() {
            if (!$(".pop").is(":hidden")) {
                $(".pop").hide();
            }
        });
    })
    </script>
</body>

</html>

效果圖:

 第二:

var publicPopWrap = $("#publicPopWrap");//彈窗外層
        var popShowBtn = $("#popShowBtn");//獲取點擊按鈕
        popShowBtn.on("click",function(){
            publicPopWrap.show();
        })
        //除了彈窗內容框內,點擊任意位置彈窗隱藏
        publicPopWrap.on("click",function(e){
            if ($(e.target).closest("#publicPop").length > 0) {
                $(this).show();
            } else {
                $(this).hide();
            }
        })

//點擊空白處隱藏彈出層

$(document).click(function(event){
                let _con = $("#zhwnlPopCon") // 設置目標區域
                if(!_con.is(event.target) && _con.has(event.target).length === 0){ // Mark 1
                    bindPopupZhwnl.hide();
                }
            });
/* Mark 1 的原理: 判斷點擊事件發生在區域外的條件是: 1. 點擊事件的對象不是目標區域本身 2. 事件對象同時也不是目標區域的子元素 */ 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM