2010-05-28 123 views
6

我有form里面的东西一个div像Jquery-隐藏的div

<form> 
<div> 
showing some information here 
</div> 

<div id="idshow" style="display:none"> 
information here 
</div> 

</form> 

我是在一些按钮单击事件内格(idshow)人口信息。当我在外部div(idshow)之外点击时我想要什么,它应该隐藏。就像我点击菜单,然后菜单显示,当我点击菜单外面它隐藏。 我需要利用一切手段,jQuery的

回答

11
$(document).click(function(event) { 
    var target = $(event.target); 

    // Check to see if the target is the div. 
    if (!target.is("div#idshow")) { 
    $("div#idshow").hide(); 
    // Prevent default event -- may not need this, try to see 
    return(false); 
    } 
}); 
3

你可以做你想做的是这样的:

$(document).click(function() { 
    $("#idshow").hide(); 
}); 
$("#idshow").click(function(e) { 
    e.stopPropagation(); 
}); 

这里发生的当您单击是,该click事件一路向上冒泡到document,如果到达那里我们隐藏了<div>。当你点击里面那<div>虽然,你可以通过使用event.stopPropagation() ...阻止气泡达到document ...所以.hide()不会触发,简短和简单:)