1

现在,当我单击“x”时,我正在关闭警报框,但我想在单击外部时关闭警报框。

请在此处查看我的代码:http: //jsfiddle.net/Ur5Xn/

如何关闭警报框点击它之外?

jQuery:

$(document).ready(function(){
    function showAlertBox(){
     $("#alert").css("display","inherit");
     $("#content").addClass("back");
    }
    function removeAlertBox(){
        $("#alert").css("display","none");
         $("#content").removeClass("back");        
    }

    $("#alertClose").click(function(){
       removeAlertBox(); 
    });
    $("#alertShow").click(function(){
       showAlertBox(); 
    });
});
4

4 回答 4

2

试试这个代码

$(document).ready(function(){
    function showAlertBox(){
     $("#alert").css("display","inherit");
     $("#content").addClass("back");
    }
    function removeAlertBox(){
        $("#alert").css("display","none");
         $("#content").removeClass("back");        
    }

    $("#alertClose").click(function(e){
        e.stopPropagation();
       removeAlertBox(); 
    });
    $("#alertShow").click(function(e){
        e.stopPropagation();
       showAlertBox(); 
    });

    $(document).click(function(e){
        removeAlertBox();
    });
});
于 2013-10-24T13:19:10.713 回答
2

这应该工作:http: //jsfiddle.net/LagBE/

$(document).on('mouseup', function (e)
{
    var alert = $('#alert');

     // Make sure neither the alert, 
     // nor anything inside of it was clicked

    if (!alert.is(e.target) && alert.has(e.target).length === 0)
    {
        removeAlertBox();
    }
});
于 2013-10-24T13:03:06.283 回答
1

这是更新工作代码:

$(document).ready(function () {
    function showAlertBox() {
        $("#alert").css("display", "inherit");
        $("#content").addClass("back");
        return false;
    }

    function removeAlertBox() {
        $("#alert").css("display", "none");
        $("#content").removeClass("back");
        return false;
    }

    $("#alertClose").click(removeAlertBox);
    $("#alertShow").click(showAlertBox);
    $('#alert').click(false);
    $(document).click(removeAlertBox);
});

http://jsfiddle.net/Ur5Xn/34/

于 2013-10-24T13:04:40.477 回答
1

jsfiddle在这里

html:

<div id="alert">
    <div id='transBG'></div>
    <div id="alertbox">I am an alert box <span id="alertClose">X</span></div>
</div>
<div id="content">
    Content <span id="alertShow">Show Alert Box</span>
</div>

CSS:

#alert {
    display:none;
}
#alertbox{
    border:1px solid #000;   
    position:fixed;
    top: 50%;
    left: 50%;
    margin-top: -50px;
    margin-left: -100px;
    height:100px;
    width:200px;
     z-index:9;
}
#transBG{
    position:fixed;
    top:0;
    left:0;
    width:100%;
    height:100%;       
    z-index:8;  
}
.back{
    opacity:0.5;
}

javascript:

$(document).ready(function(){
    function showAlertBox(){
     $("#alert").css("display","inherit");
     $("#content").addClass("back");
    }
    function removeAlertBox(){
        $("#alert").css("display","none");
         $("#content").removeClass("back");        
    }      

    $("#transBG").click(function(){
       removeAlertBox(); 
    });

    $("#alertClose").click(function(){
       removeAlertBox(); 
    });
    $("#alertShow").click(function(){
       showAlertBox(); 
    });
});
于 2013-10-24T13:04:17.910 回答