点击div触发绑定$('body'),点击 [英] clicking on div fires binding for $('body').click
问题描述
所以我做了
$('body')点击(function(){
if(loginOpened)
{
$('#loginWindow')。animate({
'width':'0px',
' height':'0px'
},fast);
}
loginOpened = false;
});
但是,即使我点击div本身也会触发事件,是否有防止这种情况
您可以使用
e.stopPropagation();
如果有一个点击事件绑定到< div />
标签。
防止事件冒泡起来
的DOM树,防止任何父
处理程序被通知的
事件。
否则,您可以检查身体内部事件的目标点击。检查 event.target
是否与您的div相同。
请参阅 event.target
I have a div, I want to set it so that when I click on something else, it would hide the div.
So I did
$('body').click(function(){
if(loginOpened)
{
$('#loginWindow').animate({
'width':'0px',
'height':'0px'
},"fast");
}
loginOpened=false;
});
However, even when I click in the div itself the event is fired, is there anyway to prevent this?
You can stop it using
e.stopPropagation();
if there is a click event bound to the <div />
tag.
Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event.
Otherwise you can check the target of the event inside the body click. Check whether event.target
is the same as your div.
See event.target
这篇关于点击div触发绑定$('body'),点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!