我隱藏與.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