本文實例講述了jQuery實現點擊自身以外區域關閉彈出層功能。分享給大家供大家參考,具體如下:
原理參考前面一篇《JS使用遮罩實現點擊某區域以外時彈窗的彈出與關閉功能》,小編順便對原文區域外點擊bug進行了修改,具體代碼如下:
<!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><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>www.49028c.com jQuery點擊關閉彈出層</title><style>.area{width:100%;height:100%;position:fixed;z-index:1;}.hide{display:none;}.con{width:400px; height:200px; background-color: #F6F4F0;position:fixed;z-index:2;}</style></head><body><div class="area hide"></div><div class="down">click</div><div class="con hide">show-area</div><script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script><script type="text/javascript">$(document).ready(function() { $("div.down").click(function(e) { e.stopPropagation(); $("div.con").removeClass("hide"); $("div.area").removeClass("hide"); }); $(".area").click(function() { if (!$("div.con").hasClass("hide")) { $("div.con").addClass("hide"); $("div.area").addClass("hide"); } });});</script></body></html>
運行該代碼后,點擊頁面上的click可彈出一個彈出層,再點擊彈出層外的空白區域即可關閉彈出層。
使用在線HTML/CSS/JavaScript代碼運行工具:http://tools.VeVB.COm/code/HtmlJsRun測試運行效果如下:
更多關于jQuery相關內容感興趣的讀者可查看本站專題:《jQuery窗口操作技巧總結》、《jQuery拖拽特效與技巧總結》、《jQuery常用插件及用法總結》、《jQuery表格(table)操作技巧匯總》、《jQuery擴展技巧總結》、《jQuery常見經典特效匯總》及《jquery選擇器用法總結》
希望本文所述對大家jQuery程序設計有所幫助。
新聞熱點
疑難解答