现在,当我点击“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();
});
});
答案 0 :(得分: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();
}
});
答案 1 :(得分: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();
});
});
答案 2 :(得分:1)
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();
});
});
答案 3 :(得分: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);
});