Jquery-隐藏div

Pan*_*kaj 13 jquery

我有一个div内部形式的东西

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

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

</form>
Run Code Online (Sandbox Code Playgroud)

我是一些按钮点击事件中div(idshow)内的人口信息.什么我想要什么,当我生病点击外div(idshow),它应该隐藏.就像我点击菜单然后菜单显示,当我点击外面的菜单它隐藏.我需要使用jquery的一切

Tau*_*ren 19

$(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 );
  }
});
Run Code Online (Sandbox Code Playgroud)


Nic*_*ver 5

您可以这样操作:

$(document).click(function() {
  $("#idshow").hide();
});
$("#idshow").click(function(e) {
  e.stopPropagation();
});
Run Code Online (Sandbox Code Playgroud)

这是发生的情况,当您单击时,click事件会一直持续到冒泡document,如果到达该事件,我们将隐藏<div>。当您在里面单击时<div>,可以document使用event.stopPropagation()... 阻止气泡上升,因此.hide()不会触发,简短而简单的:)