jquery三個關閉彈出層的小示例
更新時間:2013年11月05日 16:41:07 作者:
三個關閉彈出層的實例方法
在開發(fā)應用中我們做了一個彈出層,有時我們會做一個關閉按鈕,這樣點擊關閉就可以把彈出層關閉了,但是有時希望只要不點擊彈出層內(nèi)就自動關閉彈出層了,下面我總結了三個實例。
例1
復制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>點擊空白處關閉彈出窗口</title>
<meta http-equiv="content-type" content="text/html;charset=gb2312">
<style type="text/css">
.pop{width:200px;height:130px;background:#080;}
</style>
<script type="text/javascript" src="/ajaxjs/jquery-1.6.2.min.js"></script>
<script type="text/javascript">
$(function(){
$(document).bind("click",function(e){
var target = $(e.target);
if(target.closest(".pop").length == 0){
$(".pop").hide();
}
})
})
</script>
</head>
<body>
<div class="pop"></div>
</body>
</html>
例2,點擊自身以外地方關閉彈出層
復制代碼 代碼如下:
<html>
<style>
.hide{display:none;}
</style>
<script type="text/javascript" src="jquery-1.6.1.min.js"></script>
<script type="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>
<div class="down">click</div>
<div class="con hide">show-area</div>
</body>
</html>
例3
復制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jQuery點擊空白處關閉彈出層</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<style type="text/css">
#box{width:300px;height:200px;border:1px solid #000;display:none; margin:0 auto;}
.btn{color:red;}
</style>
<script src="http://www.honoer.com/Public/Js/jQuery/jquery-1.6.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
$(".btn").click(function(event){
var e=window.event || event;
if(e.stopPropagation){
e.stopPropagation();
}else{
e.cancelBubble = true;
}
$("#box").show();
});
$("#box").click(function(event){
var e=window.event || event;
if(e.stopPropagation){
e.stopPropagation();
}else{
e.cancelBubble = true;
}
});
document.onclick = function(){
$("#box").hide();
};
})
</script>
</head>
<body>
<div id="box">打開我了,點空白關閉啊,謝謝</div>
<span class="btn">打開彈出層</span>
</body>
</html>
相關文章
jQuery實現(xiàn)平滑滾動頁面到指定錨點鏈接的方法
這篇文章主要介紹了jQuery實現(xiàn)平滑滾動頁面到指定錨點鏈接的方法,涉及jquery鼠標事件及頁面滾動的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-07-07
Jquery實現(xiàn)彈性滑塊滑動選擇數(shù)值插件
有時我們在頁面上需要選擇數(shù)值范圍,如購物時選取價格區(qū)間,購買主機時自主選取CPU,內(nèi)存大小配置等,使用直觀的滑塊條直接選取想要的數(shù)值大小即可,無需手動輸入數(shù)值,操作簡單又方便。2015-08-08
jquery中獲得$.ajax()事件返回的值并添加事件的方法
如果想獲得$.ajax()中返回的值,直接用在success:funciton(){return xx} 是不可以的,要想獲得xx的值,要在script中,使用全局變量。利用全局變量引出xx的值。2010-04-04
jquery創(chuàng)建并行對象或者合并對象的實現(xiàn)代碼
如果有對象A ,B 現(xiàn)在我想要合并成對象C 從C里面可以找到A , B 及其子對象 怎么做2012-10-10
jQuery實現(xiàn)的解析本地 XML 文檔操作示例
這篇文章主要介紹了jQuery實現(xiàn)的解析本地 XML 文檔操作,結合實例形式分析了jQuery針對本地 XML 文檔的解析及ajax交互相關操作技巧,需要的朋友可以參考下2020-04-04
jQuery Tags Input Plugin(添加/刪除標簽插件)詳解
本文主要介紹jQuery Tags Input Plugin添加/刪除標簽插件的用法,非常實用,有需要的朋友可以參考一下。2016-06-06

