我試圖在單擊鏈接時顯示#subscribe-pop div,並在單擊任何位置時隱藏它。我可以得到它的顯示和隱藏,如果我改變:顯示div一旦點擊後隱藏,當點擊外部時隱藏
$('document').click(function() {
TO
$('#SomeOtherRandomDiv').click(function() {
HTML:
<div id="footleft">
<a href="#" onclick="toggle_visibility('subscribe-pop');">Click here to show div</a>
<div id="subscribe-pop"><p>my content</p></div>
</div>
腳本:
<script type="text/javascript">
function toggle_visibility(id) {
var e = document.getElementById("subscribe-pop");
if(e.style.display == 'block')
e.style.display = 'none';
else
e.style.display = 'block';
}
}
$('document').click(function() {
$('#subscribe-pop').hide(); //Hide the menus if visible
});
$('#subscribe-pop').click(function(e){
e.stopPropagation();
});
</script>
http://stackoverflow.com/questions/152975/how-to-detect-a-click-outside-an-element – 2013-04-24 13:36:39
使用'$(document)',而不是'$('document')' – Ian 2013-04-24 13:50:51