我隐藏与.toggle()如何在点击页面上的任何位置时隐藏切换div?
<div style="position: absolute; top: 0; right: 250px;z-index:7">
<div class="Search" style="display: none;" >
<table width="100%" style="border: 1px solid #fff; border-radius:5px;padding:15px">
<tr>
<td>
<asp:DropDownList ID="CategoryDropDownList" runat="server" />
</td>
</tr>
</table>
</div>
<div id="showSreachDiv">
<a style="cursor: pointer">
<img src="images/btnSearch.png" /></a>
</div>
</div>
/显示元素与jQuery
$(document).ready(function() {
$("#showSreachDiv").click(function() { $(".Search").toggle("slow"); });
});
当我点击我的DIV,显示切换股利。
但我想要的,当点击页面上的任何位置时隐藏切换div。
我使用此代码
$(function() {
// body click
$("body").click(function() {
// element to toggle
var $el = $(".Search");
// toggle div
if ($el.is(":visible")) {
// fade out
$el.fadeOut(200);
} else {
// fade in
$el.fadeIn(200);
}
});
});
但它显示和隐藏的toogle DIV当我点击我的股利。
但不透明页面之间的DIV叠加事件传播,然后听它的点击 - 这将是简单的。 – moonwave99 2013-02-09 10:13:57
切换document.click事件中的DIV,单击DIV本身时它会被触发。 – Niloo 2013-02-09 10:15:11
要真正理解发生了什么,您应该阅读一般事件委派。这里有一个来自Jquery的链接:http://learn.jquery.com/events/event-delegation/ – 2015-09-11 05:03:17