jQuery在页面上的任何位置单击时隐藏元素 [英] jQuery hide element when clicked anywhere on the page
本文介绍了jQuery在页面上的任何位置单击时隐藏元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道这是否是在页面上的任何位置单击时隐藏可见元素的正确方法。
I would like to know whether this is the correct way of hiding visible elements when clicked anywhere on the page.
$(document).click(function (event) {
$('#myDIV:visible').hide();
});
当单击事件发生在边界内时,元素(div,span等)不应消失元素。
The element (div, span etc) shouldn't disappear when click event occurs within the boundaries of the element.
推荐答案
如果我明白你想要隐藏div,当你点击任何地方,但是,如果你点击在div上,那么它不应该关闭。你可以这样做:
if i understand you want to hide a div when you click anywhere but the div, and if you do click while over the div, then it should NOT close. you can do this:
$(document).click(function() {
alert("me");
});
$(".myDiv").click(function(e) {
e.stopPropagation(); // This is the preferred method.
return false; // This should not be used unless you do not want
// any click events registering inside the div
});
将点击绑定到整个页面,但如果您点击相关的div将取消点击事件。
This binds the click to the entire page, but if you click on the div in question it will cancel the click event.
这篇关于jQuery在页面上的任何位置单击时隐藏元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文