JQ弹出层点击空白关闭怎么写?
<!DOCTYPEhtml><htmllang="zh-cmn-Hans"><head><metacharset="utf-8"><scripttype="text/ja...
<!DOCTYPE html><html lang="zh-cmn-Hans"><head> <meta charset="utf-8"> <script type="text/javascript" charset="utf-8" src="/Public/Js/Jquery.min.js"></script> <style type="text/css"> .box { height: 400px; width: 400px; position: absolute; left:300px; top:150px; background: red; display: none; } .btn { cursor: pointer; } </style> <script type="text/javascript"> $(function(){ $(".btn").click(function(event){ event.stopPropagation(); $(".box").show(); }); $(document).click(function(event){ if (!$(this).hasClass("box")){ $(".box").hide(); } event.stopPropagation(); //阻止事件冒泡 }); }); </script></head><body><div class="btn">弹出按钮</div><div class="box">弹出层</div></body></html>就是点击弹出按钮和弹出层以外的地方就关闭弹出层。我自己试了一下网上的,不知道到怎么的还是没用、点击弹出层内容就会关闭弹出层。那还弹出个毛线。懊恼
展开
2个回答
展开全部
<!DOCTYPE html>
<html lang="zh-cmn-Hans">
<head>
<meta charset="utf-8">
<script type="text/javascript" charset="utf-8" src="file:///E|/资料/jquery库/新建文件夹/jquery-1.7.2.js"></script>
<style type="text/css">
.box {
height: 400px;
width: 400px;
position: absolute;
left:300px;
top:150px;
background: red;
display: none;
z-index:2;
}
.btn {
cursor: pointer;
}
.boaguo{ position:absolute; background:rgba(0,0,0,0.5); height:100%; width:100%; top:0; left:0; display:none; z-index:1;}
</style>
<script type="text/javascript">
$(function(){
$(".btn").click(function(event){
event.stopPropagation();
$(".box").show();
$(".boaguo").show();
});
$(".boaguo").click(function(event){
$(".boaguo").hide();
$(".box").hide();
event.stopPropagation(); //阻止事件冒泡
});
});
</script>
</head>
<body>
<div class="btn">弹出按钮</div>
<div class="boaguo"></div>
<div class="box">弹出层</div>
</body>
</html>
<html lang="zh-cmn-Hans">
<head>
<meta charset="utf-8">
<script type="text/javascript" charset="utf-8" src="file:///E|/资料/jquery库/新建文件夹/jquery-1.7.2.js"></script>
<style type="text/css">
.box {
height: 400px;
width: 400px;
position: absolute;
left:300px;
top:150px;
background: red;
display: none;
z-index:2;
}
.btn {
cursor: pointer;
}
.boaguo{ position:absolute; background:rgba(0,0,0,0.5); height:100%; width:100%; top:0; left:0; display:none; z-index:1;}
</style>
<script type="text/javascript">
$(function(){
$(".btn").click(function(event){
event.stopPropagation();
$(".box").show();
$(".boaguo").show();
});
$(".boaguo").click(function(event){
$(".boaguo").hide();
$(".box").hide();
event.stopPropagation(); //阻止事件冒泡
});
});
</script>
</head>
<body>
<div class="btn">弹出按钮</div>
<div class="boaguo"></div>
<div class="box">弹出层</div>
</body>
</html>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
给你分享个案例:
<style type="text/css">
/* reset */
*{margin:0;padding:0;list-style-type:none;}
a{blr:expression(this.onFocus=this.blur())}/*去掉a标签的虚线框,避免出现奇怪的选中区域*/
:focus{outline:0;}
label{cursor:pointer;}
img{vertical-align:middle;}
table{empty-cells:show;border-collapse:collapse;border-spacing:0;}
h1{font-size:16px;}h2,h3,h4{font-size:14px;}h5,h6{font-size:12px;}
abbr,acronym{border:0;font-variant:normal}
address,caption,cite,code,dfn,em,th,var,optgroup{font-style:normal;font-weight:normal}
input,button,textarea,select,optgroup,option{font-family:inherit;font-size:inherit;font-style:inherit;font-weight:inherit}
input,button,textarea,select{*font-size:100%}
a,img{border:0;}
a,a:visited{color:#5e5e5e; text-decoration:none;}
a:hover{color:#4183C4;text-decoration:underline;}
.clear{display:block;overflow:hidden;clear:both;height:0;line-height:0;font-size:0;}
.clearfix:after{content:".";display:block;height:0;clear:both;visibility:hidden;}
.clearfix{display:inline-table;}/* Hides from IE-mac \*/
*html .clearfix{height:1%;}
.clearfix{display:block;}/* End hide from IE-mac */
*+html .clearfix{min-height:1%;}
body{font:12px/180% Arial,Lucida,Verdana,"宋体",Helvetica,sans-serif;color:#333;background:#fff;}
/* shortcut */
.shortcut{height:32px;line-height:32px;font-size:12px;background:#eee;text-transform:uppercase;box-shadow:1px 0px 2px rgba(0,0,0,0.2);border-bottom:1px solid #DDDDDD;}
.shortcut h1{font-size:14px;font-family:"微软雅黑","宋体";}
.shortcut a,.shortcut h1{padding:0px 10px;letter-spacing:1px;color:#333;text-shadow:0px 1px 1px #fff;display:block;float:left;width:360px;height:32px;overflow:hidden;}
.shortcut a:hover{background:#fff;}
.shortcut span.right{float:right;display:inline-block;width:468px;height:15px;overflow:hidden;margin:8px 5px 0 0;}
.shortcut .close{
background:url(http://www.hnjfbw.com/s/close-demo.gif) no-repeat 0 0;width:20px;height:20px;line-height:99em;overflow:hidden;display:inline-block;margin:6px 10px 0 10px;float:right;padding:0;
-webkit-transition:all .3s ease-out 0s;
-moz-transition:all .3s ease-out 0s;
-ms-transition:all .3s ease-out 0s;
-o-transition:all .3s ease-out 0s;
transition:all .3s ease-out 0s;
}
.shortcut .close:hover{background:url(/Public/images/close-demo.gif) no-repeat 0 -20px}
.shortcut .close:active{background:url(/Public/images/close-demo.gif) no-repeat 0 -40px}
</style>
重点是最后面那个4行样式,不通过JQ直接通过CSS来实现的
或者用
<span class="close_x" onclick="document.getElementById('gkx').style.display='none';">×</span>
希望可以帮助到你
<style type="text/css">
/* reset */
*{margin:0;padding:0;list-style-type:none;}
a{blr:expression(this.onFocus=this.blur())}/*去掉a标签的虚线框,避免出现奇怪的选中区域*/
:focus{outline:0;}
label{cursor:pointer;}
img{vertical-align:middle;}
table{empty-cells:show;border-collapse:collapse;border-spacing:0;}
h1{font-size:16px;}h2,h3,h4{font-size:14px;}h5,h6{font-size:12px;}
abbr,acronym{border:0;font-variant:normal}
address,caption,cite,code,dfn,em,th,var,optgroup{font-style:normal;font-weight:normal}
input,button,textarea,select,optgroup,option{font-family:inherit;font-size:inherit;font-style:inherit;font-weight:inherit}
input,button,textarea,select{*font-size:100%}
a,img{border:0;}
a,a:visited{color:#5e5e5e; text-decoration:none;}
a:hover{color:#4183C4;text-decoration:underline;}
.clear{display:block;overflow:hidden;clear:both;height:0;line-height:0;font-size:0;}
.clearfix:after{content:".";display:block;height:0;clear:both;visibility:hidden;}
.clearfix{display:inline-table;}/* Hides from IE-mac \*/
*html .clearfix{height:1%;}
.clearfix{display:block;}/* End hide from IE-mac */
*+html .clearfix{min-height:1%;}
body{font:12px/180% Arial,Lucida,Verdana,"宋体",Helvetica,sans-serif;color:#333;background:#fff;}
/* shortcut */
.shortcut{height:32px;line-height:32px;font-size:12px;background:#eee;text-transform:uppercase;box-shadow:1px 0px 2px rgba(0,0,0,0.2);border-bottom:1px solid #DDDDDD;}
.shortcut h1{font-size:14px;font-family:"微软雅黑","宋体";}
.shortcut a,.shortcut h1{padding:0px 10px;letter-spacing:1px;color:#333;text-shadow:0px 1px 1px #fff;display:block;float:left;width:360px;height:32px;overflow:hidden;}
.shortcut a:hover{background:#fff;}
.shortcut span.right{float:right;display:inline-block;width:468px;height:15px;overflow:hidden;margin:8px 5px 0 0;}
.shortcut .close{
background:url(http://www.hnjfbw.com/s/close-demo.gif) no-repeat 0 0;width:20px;height:20px;line-height:99em;overflow:hidden;display:inline-block;margin:6px 10px 0 10px;float:right;padding:0;
-webkit-transition:all .3s ease-out 0s;
-moz-transition:all .3s ease-out 0s;
-ms-transition:all .3s ease-out 0s;
-o-transition:all .3s ease-out 0s;
transition:all .3s ease-out 0s;
}
.shortcut .close:hover{background:url(/Public/images/close-demo.gif) no-repeat 0 -20px}
.shortcut .close:active{background:url(/Public/images/close-demo.gif) no-repeat 0 -40px}
</style>
重点是最后面那个4行样式,不通过JQ直接通过CSS来实现的
或者用
<span class="close_x" onclick="document.getElementById('gkx').style.display='none';">×</span>
希望可以帮助到你
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询