Jquery-隐藏div

我有一个div内部forms的东西

showing some information here
information here

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

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

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

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

这里发生的是当你点击时, click事件一直冒泡到document ,如果它到达那里我们隐藏

。 当您在

单击时,可以通过使用event.stopPropagation()来阻止气泡到达document …所以.hide()不会触发,简短而简单:)