jQuery除指定区域外点击任何地方隐藏DIV功能

238 阅读1分钟
$('body').click(function(e) {  var target = $(e.target);  // 如果#overlay或者#btn下面还有子元素,可使用  // !target.is('#btn *') && !target.is('#overlay *')  if(!target.is('#btn') && !target.is('#overlay')) {    if ( $('#overlay').is(':visible') ) {      $('#overlay').hide();                             }  }});或者?123456$('body').click(function(e) {  if(e.target.id != 'btn' && e.target.id != 'overlay')   if ( $('#overlay').is(':visible') ) {     $('#overlay').hide();   }})PS:下面在接着看一段代码jquery 点击除本身外其他地方隐藏?123456789$("#test").click(function(e) {  e?e.stopPropagation():event.cancelBubble = true;});$(document).click(function() {  $("#test").fadeOut();<pre name="code" class="html">e?e.stopPropagation():event.cancelBubble = true;  为阻止冒泡事件</pre> });<pre></pre><br><link rel="stylesheet" href="http://static.blog.csdn.net/public/res-min/markdown_views.css?v=2.0">