关于javascript:如果没有隐藏div,请点击任意隐藏

If div is not hidden click anywhere to hide

本问题已经有最佳答案,请猛点这里访问。

我尝试在默认情况下隐藏一个DIV,并通过单击一个按钮来显示。要关闭DIV,我可以单击按钮或屏幕上的其他任何地方。下面是我的尝试,但关闭部分不起作用。如果有人能给我指出正确的实现方法或者更好的方法,我会很感激。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$('#theDiv').hide();

$("#showDivBtn").click(function(){
  $("#theDiv").show();
});

if ( !$('#theDiv:hidden') ) {

$(document).click(function() {
    $('#theDiv').hide();
});
$('#theDiv').click(function(e) {
    e.stopPropagation();
    return false;        
});

}

});


(P)Placing the entire event handler inside a condition only checks the condition on on first pages,and the event handler is probably never attached,try it like this instead:(p)字母名称(P)Fiddle(p)


(P)试试这个(p)字母名称


(P)I don't think you can target EDOCX1 plography 2 with a EDOCX1 penal 3 handler like that.(p)(P)Rather than making it so you can literally clik anywhere to close the div,just make it seem that way.Put a clear div behind the one that you want to be able to close and make it cover the whole screen.他们可以攻击你的点击手。(p)(P)HTML:(p)字母名称(P)Javascript:(p)字母名称(P)CSS:(p)字母名称(P)为了演示目的,我制造了背景不同可见于此(p)


(P)试试这个(p)字母名称


(P)Try Using(p)(P)字母名称(p)(P)Instead of(p)(P)字母名称(p)