在特定div之外的页面上的任意位置单击事件 [英] Event on a click everywhere on the page outside of the specific div
本文介绍了在特定div之外的页面上的任意位置单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当用户点击该div之外的页面上的任何位置时,我想隐藏div。如何使用原始javascript或jQuery?
I'd like to hide a div when user click anywhere on the page outside of that div. How can I do that using raw javascript or jQuery?
推荐答案
将单击事件附加到文档以隐藏div:
Attach a click event to the document to hide the div:
$(document).click(function(e) {
$('#somediv').hide();
});
将一个点击事件附加到div,以阻止点击它传播到文档:
Attach a click event to the div to stop clicks on it from propagating to the document:
$('#somediv').click(function(e) {
e.stopPropagation();
});
这篇关于在特定div之外的页面上的任意位置单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文