csdnliuy 2017-11-13
具体代码如下所示:
$('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();
}
}
});或者
$('body').click(function(e) {
if(e.target.id != 'btn' && e.target.id != 'overlay')
if ( $('#overlay').is(':visible') ) {
$('#overlay').hide();
}
})PS:下面在接着看一段代码jquery 点击除本身外其他地方隐藏
$("#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">总结
本文实例讲述了jQuery基于闭包实现的显示与隐藏div功能。分享给大家供大家参考,具体如下:。<p>当前手机号码:<span id="oldPhone">$!{user.phone}</span>&