我试图在单击链接时显示#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