jquery 单击li防止重复加载的实现代码


Posted in Javascript onDecember 24, 2010

因为加载内容比较慢,所以用户可能在li上不经意点击了两次,那么就会请求两次,这是我们不想看到的。
今天在javascript-jquery群上一筒子发了两个demo给我,他的方法是先将单击的li节点拷贝出来,在加载完后
在重新插回去,显然不太适合我做的功能。
正一筹莫展,想了一下,何不在li点击时加入loading图片(在ajax加载前),判断li节点上是否存在了img元素,
没有则加载ajax内容,否则不处理。
测试了可以通过,\(^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); // 设置侧边栏的高度 
$(window).resize(function(){setHeight("#siderbar",130)}); //窗口变化时重新设置侧边栏高度 
$.get("sider.html",function(data){ 
$('#siderbar_box').append(data); 
}); 
/*设置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去显示主要内容 
}); }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(); 
} 
} 
}); 
//content显示列表标题 
function showChapter(){ 
$.get("chapter.html",function(data){ 
$("#content").html(data); 
$("#content ul li").live("click",function(){ //绑定加载后的标题单击事件 
$current=$(this); 
if($current.children("table").attr("id")==undefined) //单击标题,第一次ajax加载 
{ 
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<span>©copyright by <a href="http://cnblogs.com/tomieric">Tomi-Eric'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.3water.com/js/jquery_li_click/demo.html
打包下载 http://xiazai.3water.com/201012/yuanma/jquery_li_click.rar
Javascript 相关文章推荐
有趣的javascript数组定义方法
Sep 10 Javascript
js的Boolean对象初始值示例
Mar 04 Javascript
javascript数字时钟示例分享
Apr 23 Javascript
BootStrap中Datetimepicker和uploadify插件应用实例小结
May 26 Javascript
JS中关于事件处理函数名后面是否带括号的问题
Nov 16 Javascript
jquery对象与DOM对象转化
Feb 08 Javascript
js判断PC端与移动端跳转
Dec 24 Javascript
详解tween.js 中文使用指南
Jan 05 Javascript
vue 实现通过手机发送短信验证码注册功能
Apr 19 Javascript
jQuery实现的简单歌词滚动功能示例
Jan 07 jQuery
解决vue net :ERR_CONNECTION_REFUSED报错问题
Aug 13 Javascript
Vue如何清空对象
Mar 03 Vue.js
基于jquery的关于动态创建DOM元素的问题
Dec 24 #Javascript
在JavaScript中获取请求的URL参数
Dec 22 #Javascript
基于Jquery的表格隔行换色,移动换色,点击换色插件
Dec 22 #Javascript
jQuery Clone Bug解决代码
Dec 22 #Javascript
修改jquery.lazyload.js实现页面延迟载入
Dec 22 #Javascript
jquery插件 autoComboBox 下拉框
Dec 22 #Javascript
Jquery截取中文字符串的实现代码
Dec 22 #Javascript
You might like
PHP4在Windows2000下的安装
2006/10/09 PHP
require(),include(),require_once()和include_once()区别
2008/03/27 PHP
PHP-MySQL教程归纳总结
2008/06/07 PHP
实现PHP多线程异步请求的3种方法
2014/01/17 PHP
PHP中unset,array_splice删除数组中元素的区别
2014/07/28 PHP
PHP数组相关函数汇总
2015/03/24 PHP
PHP 计算至少是其他数字两倍的最大数的实现代码
2020/05/26 PHP
How to Auto Include a Javascript File
2007/02/02 Javascript
jQuery 版本的文本输入框检查器Input Check
2009/07/09 Javascript
Js 去掉字符串中的空格(实现代码)
2013/11/19 Javascript
js delete 用法(删除对象属性及变量)
2014/08/24 Javascript
AngularJS仿苹果滑屏删除控件
2016/01/18 Javascript
JS获取当前使用的浏览器名字以及版本号实现方法
2016/08/19 Javascript
如何给ss bash 写一个 WEB 端查看流量的页面
2017/03/23 Javascript
node文件上传功能简易实现代码
2017/06/16 Javascript
基于vue中css预加载使用sass的配置方式详解
2018/03/13 Javascript
Python 随机生成中文验证码的实例代码
2013/03/20 Python
在Python中用get()方法获取字典键值的教程
2015/05/21 Python
python 实时遍历日志文件
2016/04/12 Python
python导出hive数据表的schema实例代码
2018/01/22 Python
python实现写数字文件名的递增保存文件方法
2018/10/25 Python
Python3+Requests+Excel完整接口自动化测试框架的实现
2019/10/11 Python
Python实现图片裁剪的两种方式(Pillow和OpenCV)
2019/10/30 Python
python getopt模块使用实例解析
2019/12/18 Python
基于python实现地址和经纬度转换
2020/05/19 Python
Python urllib2运行过程原理解析
2020/06/04 Python
python Socket网络编程实现C/S模式和P2P
2020/06/22 Python
video实现有声音自动播放的实现方法
2020/05/20 HTML / CSS
益模软件Java笔试题
2012/03/27 面试题
标准毕业生自荐信范文
2013/11/04 职场文书
债务纠纷委托书
2014/08/30 职场文书
2014年度个人工作总结
2014/11/07 职场文书
律政俏佳人观后感
2015/06/09 职场文书
红楼梦读书笔记
2015/06/25 职场文书
Mysql文件存储图文详解
2021/06/01 MySQL
MySQL系列之十一 日志记录
2021/07/02 MySQL