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 wor开发者_StackOverflow社区k 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?
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)
An easy way to do this is to cover your page with a transparent (could also be completely transparent) mask using a div
.
Attach a click
handler on this mask div
to close out the popup.
See example here: http://www.sohtanaka.com/web-design/examples/modal-window/
Here is an example, using the Event.target (cross browser).
http://jsbin.com/eqeto3/edit
精彩评论