You could bind a click
event to the document
and check if the click event happend within that div
or any children. If not, close it.
Plugin time!
(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.body).unbind(ename, tempo);
}
});
});
};
}(jQuery));
usage:
$('.panel-tab').outside('click', function() {
$('#panel').stop(true, true).slideUp('slow');
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…