因為載入內容比較慢,使用者很可能在 li 上不經意點了兩下,於是就發出兩次請求——這顯然不是我們想看到的。
在 jQuery 社群裡有人給了我兩個 demo,他的做法是把被點擊的 li 節點先複製出來,等載入完再插回去。這對我需要的功能來說並不適合。正當我一籌莫展時想到:何不乾脆在點擊時先塞入一張 loading 圖片,然後判斷該 li 裡有沒有 img 元素?沒有就載入,有就什麼都不做。測試之後確實可行。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jquery ajax 載入綁定事件</title>
<style>
* { margin: 0; padding: 0; }
body { font-size: 12px; font-family: Verdana, Arial, Helvetica, sans-serif; }
#container { position: relative; overflow: hidden; }
#header {
height: 100px; line-height: 100px; background: #dedede; padding-left: 20px;
position: fixed; left: 0; top: 0; width: 100%; margin-bottom: 20px;
}
#header h1 { font-size: 120%; color: #fff; }
#header h1 span { font-size: 75%; }
#siderbar {
width: 240px; margin-left: 10px; border: 1px solid #ddd;
position: fixed; left: 0; top: 120px; z-index: 9999;
}
#siderbar_box { padding: 10px; overflow: auto; }
#siderbar h4 { background: #eee; color: #666; padding: 5px 10px; }
#siderbar_box ul { list-style: none; margin-left: 10px; }
#content { padding: 120px 0 0 260px; overflow: auto; }
#content ul { list-style: none; }
#content ul li { border: 1px solid #ddd; cursor: pointer; display: block; }
#content ul li span { display: block; padding: 5px; }
#content ul li table {
margin: 5px auto; padding: 0; border-collapse: collapse;
border: 1px solid #999; width: 98%;
}
#content ul li table td { border: 1px solid #999; }
#content_footer { text-align: center; }
.focus { padding: 2px 5px; background: #fafafa; }
.highlight { color: #fff; background: #0099FF; }
</style>
<script src="jquery-1.3.2.min.js"></script>
<script>
$(function () {
setHeight("#siderbar", 130); // 設定側邊欄高度
$(window).resize(function () { setHeight("#siderbar", 130); });
$.get("sider.html", function (data) {
$('#siderbar_box').append(data);
});
function setHeight(id, h) {
var windowHeight = $(window).height();
$(id).css({ "height": (windowHeight - h) + "px" });
}
/* 綁定動態載入後的 li 點擊 */
$("#siderbar_box ul li a").live("click", function () {
var $current = $(this);
var $currentli = $(this).parent();
if ($currentli.children("ul").attr("id") == undefined) {
/* 第一次,需要 ajax 載入 */
$currentli.siblings().children("ul").slideUp('fast');
$currentli.siblings().children("a").removeClass("focus");
$.get("chapter.html", function (data) {
$current.addClass("focus").parent().append(data);
showChapter();
});
} else {
$currentli.siblings().children("ul").slideUp('fast');
$currentli.siblings().children("a").removeClass("focus");
if ($currentli.children("ul").is(":visible")) {
$current.removeClass("focus").parent().children("ul").slideUp('fast');
} else {
$current.addClass("focus").parent().children("ul").slideDown('slow');
showChapter();
}
}
});
/* 在右側內容區顯示章節列表 */
function showChapter() {
$.get("chapter.html", function (data) {
$("#content").html(data);
$("#content ul li").live("click", function () {
var $current = $(this);
if ($current.children("table").attr("id") == undefined) {
/* 第一次點擊,才 ajax 載入 */
/* 關鍵:用 img 是否已存在來防止重複請求 */
if ($current.children("span").find("img").size() < 1) {
$current.children("span").append("<img src='loading.gif' border='none'/>");
$.get("table.html", function (data) {
$current.append(data)
.children("span").addClass("highlight").find("img").remove();
});
}
} else {
if ($current.children("table").is(":visible")) {
$current.children("span").removeClass("highlight").next("table").hide();
} else {
$current.children("span").addClass("highlight").next("table").show();
}
}
});
});
}
});
</script>
</head>
<body>
<div id="container">
<div id="header"><h1>DEMO</h1></div>
<div id="siderbar">
<h4>課程</h4>
<div id="siderbar_box"></div>
</div>
<div id="content">
<div id="content_footer"></div>
</div>
</div>
</body>
</html>
線上範例:https://demo.jb51.net/js/jquery_li_click/demo.html
原始碼下載:https://xiazai.jb51.net/201012/yuanma/jquery_li_click.rar
這個技巧很通用:把 loading 圖片當成「請求進行中」的旗標,載入完成後再移除,就能避免同一個元素被重複請求。比起複製節點再插回的做法,邏輯單純得多。