jquery 單擊li防止重復(fù)加載的實(shí)現(xiàn)代碼
因?yàn)榧虞d內(nèi)容比較慢,所以用戶可能在li上不經(jīng)意點(diǎn)擊了兩次,那么就會(huì)請(qǐng)求兩次,這是我們不想看到的。
今天在javascript-jquery群上一筒子發(fā)了兩個(gè)demo給我,他的方法是先將單擊的li節(jié)點(diǎn)拷貝出來(lái),在加載完后
在重新插回去,顯然不太適合我做的功能。
正一籌莫展,想了一下,何不在li點(diǎn)擊時(shí)加入loading圖片(在ajax加載前),判斷l(xiāng)i節(jié)點(diǎn)上是否存在了img元素,
沒(méi)有則加載ajax內(nèi)容,否則不處理。
測(cè)試了可以通過(guò),\(^o^)/。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jquery ajax加載綁定事件</title>
<style>
*{ margin:0px; padding:0px;}
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:absolute; left:0px; top:0px; position:fixed!important; 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:absolute; left:0px; top:120px; position:fixed!important; 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 0px 0px 260px; overflow:auto;_padding:120px 0px 0px 280px;}
#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:0px; border-collapse:collapse; border:1px solid #999; width:98%;}
#content ul li table td{/* padding:2px 5px;*/ border:1px solid #999;}
#content_footer{ text-align:center;}
.focus{padding:2px 5px; boder:1px solid #aaa; background:#fafafa;}
.highlight{color:#fff; background:#0099FF}
</style>
<script src="jquery-1.3.2.min.js"></script>
<script>
$(function(){
setHeight("#siderbar",130); // 設(shè)置側(cè)邊欄的高度
$(window).resize(function(){setHeight("#siderbar",130)}); //窗口變化時(shí)重新設(shè)置側(cè)邊欄高度
$.get("sider.html",function(data){
$('#siderbar_box').append(data);
});
/*設(shè)置ID的高度*/
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(); //在content去顯示主要內(nèi)容
});
}else{
$currentli.siblings().children("ul").slideUp('fast');
$currentli.siblings().children("a").removeClass("focus");
if($currentli.children("ul").is(":visible")) //處于展開(kāi)狀態(tài)
{
$current.removeClass("focus").parent().children("ul").slideUp('fast');
}else{
$current.addClass("focus").parent().children("ul").slideDown('slow');
showChapter();
}
}
});
//content顯示列表標(biāo)題
function showChapter(){
$.get("chapter.html",function(data){
$("#content").html(data);
$("#content ul li").live("click",function(){ //綁定加載后的標(biāo)題單擊事件
$current=$(this);
if($current.children("table").attr("id")==undefined) //單擊標(biāo)題,第一次ajax加載
{
if($current.children("span").find("img").size()<1) //只加載一次內(nèi)容,防止多次請(qǐng)求加載
{
$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<span>©copyright by <a s</a><span></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>
演示地址 http://demo.jb51.net/js/jquery_li_click/demo.html
打包下載 http://xiazai.jb51.net/201012/yuanma/jquery_li_click.rar
- 使用JQuery和CSS模擬超鏈接的用戶單擊事件的實(shí)現(xiàn)代碼
- Jquery為單選框checkbox綁定單擊click事件
- jQuery 全選/反選以及單擊行改變背景色實(shí)例
- jquery禁用右鍵單擊功能屏蔽F5刷新
- jQuery實(shí)現(xiàn)單擊按鈕遮罩彈出對(duì)話框(仿天貓的刪除對(duì)話框)
- jQuery+ajax實(shí)現(xiàn)鼠標(biāo)單擊修改內(nèi)容的方法
- jQuery+ajax實(shí)現(xiàn)鼠標(biāo)單擊修改內(nèi)容的思路
- 一款基jquery超炫的動(dòng)畫(huà)導(dǎo)航菜單可響應(yīng)單擊事件
- jQuery實(shí)現(xiàn)多按鈕單擊變色
- 非jQuery實(shí)現(xiàn)照片散落桌子上,單擊放大的LightBox效果
- jQuery實(shí)現(xiàn)單擊和鼠標(biāo)感應(yīng)事件
相關(guān)文章
jQuery實(shí)現(xiàn)的移動(dòng)端圖片縮放功能組件示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)的移動(dòng)端圖片縮放功能組件,結(jié)合實(shí)例形式詳細(xì)分析了jQuery基于移動(dòng)端的圖片縮放功能組件實(shí)現(xiàn)原理、步驟、核心代碼及使用技巧,需要的朋友可以參考下2020-05-05jQuery實(shí)現(xiàn)輸入框下拉列表樹(shù)插件特效代碼分享
這篇文章主要介紹了jQuery實(shí)現(xiàn)輸入框下拉列表樹(shù)特效,推薦給大家,有需要的小伙伴可以參考下。2015-08-08jQuery select自動(dòng)選中功能實(shí)現(xiàn)方法分析
這篇文章主要介紹了jQuery select自動(dòng)選中功能,結(jié)合實(shí)例形式分析了jQuery實(shí)現(xiàn)select響應(yīng)與級(jí)聯(lián)菜單顯示相關(guān)功能與操作技巧,需要的朋友可以參考下2016-11-11jQuery實(shí)現(xiàn)的別踩白塊小游戲完整示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)的別踩白塊小游戲,結(jié)合實(shí)例形式詳細(xì)分析了jQuery針對(duì)頁(yè)面元素的計(jì)算、判定、動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-01-01表頭固定(利用jquery實(shí)現(xiàn)原理介紹)
表頭固定應(yīng)該是一個(gè)用得比較多的功能,參考了網(wǎng)上幾個(gè)例子,在幾個(gè)常用瀏覽器下顯示不是很完美2012-11-11jQuery 常用特效實(shí)例小結(jié)【顯示與隱藏、淡入淡出、滑動(dòng)、動(dòng)畫(huà)等】
這篇文章主要介紹了jQuery 常用特效,結(jié)合實(shí)例形式總結(jié)分析了jquery顯示與隱藏、淡入淡出、滑動(dòng)、動(dòng)畫(huà)等常用特效實(shí)現(xiàn)技巧,需要的朋友可以參考下2020-05-05