jquery三个关闭弹出层的小示例
jQuery 示例 关闭 三个 弹出
2023-06-13 09:15:11 时间
在开发应用中我们做了一个弹出层,有时我们会做一个关闭按钮,这样点击关闭就可以把弹出层关闭了,但是有时希望只要不点击弹出层内就自动关闭弹出层了,下面我总结了三个实例。
例1
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<title>点击空白处关闭弹出窗口</title>
<metahttp-equiv="content-type"content="text/html;charset=gb2312">
<styletype="text/css">
.pop{width:200px;height:130px;background:#080;}
</style>
<scripttype="text/javascript"src="/ajaxjs/jquery-1.6.2.min.js"></script>
<scripttype="text/javascript">
$(function(){
$(document).bind("click",function(e){
vartarget =$(e.target);
if(target.closest(".pop").length==0){
$(".pop").hide();
}
})
})
</script>
</head>
<body>
<divclass="pop"></div>
</body>
</html>
例2,点击自身以外地方关闭弹出层
<html>
<style>
.hide{display:none;}
</style>
<scripttype="text/javascript"src="jquery-1.6.1.min.js"></script>
<scripttype="text/javascript">
$(document).ready(function(){
$("div.down").click(function(e){
e.stopPropagation();
$("div.con").removeClass("hide");
});
$(document).click(function(){
if(!$("div.con").hasClass("hide")){
$("div.con").addClass("hide");
}
});
});
</script>
<body>
<divclass="down">click</div>
<divclass="conhide">show-area</div>
</body>
</html>
例3
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jQuery点击空白处关闭弹出层</title>
<metahttp-equiv="content-type"content="text/html;charset=utf-8">
<styletype="text/css">
#box{width:300px;height:200px;border:1pxsolid#000;display:none;margin:0auto;}
.btn{color:red;}
</style>
<scriptsrc="http://www.honoer.com/Public/Js/jQuery/jquery-1.6.2.min.js"type="text/javascript"></script>
<scripttype="text/javascript">
$(function(){
$(".btn").click(function(event){
vare=window.event||event;
if(e.stopPropagation){
e.stopPropagation();
}else{
e.cancelBubble=true;
}
$("#box").show();
});
$("#box").click(function(event){
vare=window.event||event;
if(e.stopPropagation){
e.stopPropagation();
}else{
e.cancelBubble=true;
}
});
document.onclick=function(){
$("#box").hide();
};
})
</script>
</head>
<body>
<divid="box">打开我了,点空白关闭啊,谢谢</div>
<spanclass="btn">打开弹出层</span>
</body>
</html>
相关文章
- 仿迅雷焦点广告效果(JQuery版)
- Jquery作者JohnResig自己封装的javascript常用函数
- 跟着JqueryAPI学Jquery之一选择器
- 基于jquery实现的上传图片及图片大小验证、图片预览效果代码
- Jsonp跨域的原理以及Jquery的解决方案
- jquery提交值不为空的元素示例代码
- jquery获取表单元素里面的值示例代码
- JQuery分别取得每行最后一列和最后一行的示例代码
- jQuery动画效果-slideUpslideDown上下滑动示例代码
- Jquery选中或取消radio示例
- jquery漂亮的删除确认和提交无刷新删除示例
- jquery五角星评分插件示例分享
- jquery单行文字向上滚动效果示例
- jquery实现select选中行、列合计示例
- jquery等待效果示例
- jquery提交表单mvc3后台处理示例
- jQuery判断复选框是否勾选的原理及示例
- jquery的clone方法应用于textarea和select的bug修复
- jquery操作checkbox示例分享
- jQuery删除或是清空某个HTML元素示例
- jquery实现两Select标签项互调示例代码