$(
'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();
}
})
$(
"#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"
>