简体   繁体   English

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

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

I am using the to implement a popup window using zIndex... things work well but I want to implement a function, that is when user click any region that is outside of the popup div, the div will be closed, how to do that? 我正在使用zIndex实现一个弹出窗口...效果很好,但我想实现一个功能,即当用户单击弹出div之外的任何区域时,div将关闭,该怎么做?

Bin 箱子

(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));

That is a copy&paste code from 那是来自的复制粘贴代码

mouse click somewhere else on page (not on a specific div) 鼠标单击页面其他位置(不在特定div上)

An easy way to do this is to cover your page with a transparent (could also be completely transparent) mask using a div . 一种简单的方法是使用div覆盖透明(也可能是完全透明)的蒙版。

Attach a click handler on this mask div to close out the popup. 在此遮罩div上附加click处理程序,以关闭弹出窗口。

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

Here is an example, using the Event.target (cross browser). 这是一个使用Event.target(跨浏览器)的示例。

http://jsbin.com/eqeto3/edit http://jsbin.com/eqeto3/edit

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

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