jquery文件体一次点击事件 [英] jquery document body one click event
问题描述
我有一个按钮,当它点击它显示一个特定的div。这个div有几个后代。现在,我想要的是,当我点击文档中的其他地方,而不是任何这些后代,这个div消失了。
我以为是使用不这样的选择器:
$(#button)。 function(){
$(#mydiv)。show();
$(document.body).not($(#mydiv)。children())。 ',function(e){
$(#mydiv)。hide();
});
return false;
});
但这不工作!任何想法为什么?谢谢
如何检查点击事件以查看点击的内容?具体看看 event.target
。
$(document)。点击(function(event){
var target = $(event.target);
if(!target.attr('id')。match(/ ^ mydiv /)& & target.parents('#mydiv')。length == 0){
$('#mydiv')。hide();
}
});
我有使用这段代码关闭一个打开的窗口,当有人点击任何地方,但窗口。 p>
Could somebody help me with this? :
I have a button that when clicked it shows a certain div. This div has several descendants. Now, what I want is that when I click somewhere else in the document, but not in any of those descendants that this div disappears. what I thought was to use the not selector like this:
$("#button").click(function(){
$("#mydiv").show();
$(document.body).not($("#mydiv").children()).one('click',function(e) {
$("#mydiv").hide();
});
return false;
});
but this is not working! Any idea why? thanks
How about checking the click event to see what was clicked? Specifically look at the event.target
.
$(document).click(function(event) {
var target = $(event.target);
if (!target.attr('id').match(/^mydiv/) && target.parents('#mydiv').length == 0) {
$('#mydiv').hide();
}
});
I have used this code before to close an open window when someone clicks anywhere but the window.
这篇关于jquery文件体一次点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!