javascript - jQuery实现弹出框关闭问题
PHPz
PHPz 2017-04-10 17:24:54
0
2
882

下面这个弹出框,想实现如下效果:
1、点击弹出框右上角的叉时,弹出框关闭
2、点击弹出框本身以外的地方时,弹出框关闭
3、点击弹出框本身,弹出框不关闭(那个叉除外)
Html:

<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <link href="https://cdn.bootcss.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css" rel="stylesheet">
    <style>
        #card1 {
            width: 40rem;
            position: absolute;
            display: none;
            right: 0;
            left: auto;
            top: 100%;
            z-index: 1000;
            float: left;
            min-width: 160px;
            padding: 5px 0;
            margin: 2px 0 0;
            font-size: 1rem;
            color: #373a3c;
            text-align: left;
            list-style: none;
            background-color: #fff;
            -webkit-background-clip: padding-box;
            background-clip: padding-box;
            border: 1px solid rgba(0, 0, 0, .15);
            border-radius: .25rem;
        }
    </style>
</head>
<body>
<p class="col-lg-6">
    <p class="input-group">
        <input type="text" class="form-control" aria-label="Text input with dropdown button">
        <p class="input-group-btn">
            <button id="button1" type="button" class="btn btn-secondary dropdown-toggle"
                    aria-haspopup="true" aria-expanded="false">
            </button>
            <p class="card dropdown-menu-right" id="card1">
                <p class="card-header">
                    弹出框
                    <button type="button" class="close" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                        <span class="sr-only">Close</span>
                    </button>
                </p>
                <p class="card-block">
                    <h4 class="card-title">Special title treatment</h4>
                    <p class="card-text">With supporting text below as a natural lead-in to additional content.</p>
                    <a href="#" class="btn btn-primary">Go somewhere</a>
                </p>
            </p>
        </p>
    </p>
</p>
<script src="https://cdn.bootcss.com/jquery/2.2.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/tether/1.1.1/js/tether.min.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/4.0.0-alpha.2/js/bootstrap.min.js"></script>

</body>
</html>

Javascript:


<script>
    $('#button1').click(function () {
        var menu = $('#card1');
        if (menu.is(":hidden")) {
            menu.show();
        } else {
            menu.hide();
        }
    });
</script>
PHPz
PHPz

学习是最好的投资!

reply all(2)
伊谢尔伦

我的想法是,首先阻止弹出框点击事件的冒泡,然后再给document绑定关闭弹出框的事件。
这样点击弹出框内,由于阻止了事件冒泡就不会冒到document。而点击页面其余部分可关闭该弹框。

小葫芦

<script type="text/javascript">
$(function(){

$(window).click(function(){  
   $("#card1").hide();
});
$("#button1").click(function(e){
   e.stopPropagation();
   $(this).siblings("#card1").show(); 
});
$("button.close").click(function(){
   $(this).parents("#card1").hide();    
});
$("#card1,.form-control").click(function(e){
   e.stopPropagation();
});

})
</script>

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template