This question already has answers here:
hide div if clicked outside of it [duplicate]
(4个答案)
6年前关闭。
我在各种解决方案中进行搜索,但似乎没有一个可行。为此,我在这里返回,以便与您一起找到解决方案。我创建了这种窗口来共享和自定义网站内容。
问题是,当我要关闭时,我想单击div(黑色区域)。
对不起,英语,但我是意大利语。
进一步阅读
What is event bubbling and capturing
Event order
(4个答案)
6年前关闭。
我在各种解决方案中进行搜索,但似乎没有一个可行。为此,我在这里返回,以便与您一起找到解决方案。我创建了这种窗口来共享和自定义网站内容。
问题是,当我要关闭时,我想单击div(黑色区域)。
对不起,英语,但我是意大利语。
`http://jsfiddle.net/34uLs/`
最佳答案
这个问题确实是hide div if clicked outside of it的重复项
但是,在这种情况下,由于深色背景#shead
是最顶部的元素,因此应将click事件附加到该元素。如果click事件附加到$(document)
对象,它将永远不会到达它,因为#shead
会捕获它。
解:
jsFiddle
/** The event should be binded with this element **/
$('#shead').click(function () {
$(this).toggleClass("open");
});
进一步阅读
What is event bubbling and capturing
Event order
09-16 19:34