繁体   English   中英

jQuery:如何捕获不在以下区域的click事件 <div> ?

[英]Jquery: how to capture the click event that is NOT in the region of <div>?

我正在使用zIndex实现一个弹出窗口...效果很好,但我想实现一个功能,即当用户单击弹出div之外的任何区域时,div将关闭,该怎么做?

箱子

(function($){
   $.fn.outside = function(ename, cb){
      return this.each(function(){
         var $this = $(this),
              self = this;

         $(document).bind(ename, function tempo(e){
             if(e.target !== self && !$.contains(self, e.target)){
                cb.apply(self, [e]);
                if(!self.parentNode) $(document).unbind(ename, tempo);
             }
         });
      });
   };
}(jQuery));

那是来自的复制粘贴代码

鼠标单击页面其他位置(不在特定div上)

一种简单的方法是使用div覆盖透明(也可能是完全透明)的蒙版。

在此遮罩div上附加click处理程序,以关闭弹出窗口。

在此处查看示例: http : //www.sohtanaka.com/web-design/examples/modal-window/

这是一个使用Event.target(跨浏览器)的示例。

http://jsbin.com/eqeto3/edit

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM