Jquery-隐藏div

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2928275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:33:55  来源:igfitidea点击:

Jquery- Hide div

jquery

提问by Pankaj

I have a div inside form something like

我在表单中有一个 div 类似于

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

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

</form>

i am population information inside div(idshow) on some button click event. what i want whenever i ill click outside div(idshow), it should be hide. just like i click on menu then menu is display and when i click outside menu it goes hide. I need everything using jquery

我是某个按钮单击事件中 div(idshow) 中的人口信息。每当我在 div(idshow) 外单击时我想要什么,它应该是隐藏的。就像我点击菜单然后菜单显示,当我点击菜单外时它会隐藏。我需要使用 jquery 的一切

回答by Tauren

$(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 );
  }
});

回答by Nick Craver

You can do what you want like this:

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

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

What happens here is when you click, the clickevent bubbles up all the way to document, if it gets there we hide the <div>. When you click inside that <div>though, you can stop the bubble from getting up to documentby using event.stopPropagation()...so the .hide()doesn't fire, short and simple :)

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