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 28 Javascript
jquery数组之存放checkbox全选值示例代码
Dec 20 Javascript
JavaScript如何自定义trim方法
Jul 28 Javascript
CSS3实现动态背景登录框的代码
Jul 28 Javascript
JS实现仿Windows7风格的网页右键菜单效果代码
Sep 11 Javascript
JS实现的网页背景闪电闪烁效果代码
Oct 17 Javascript
实例讲解js验证表单项是否为空的方法
Jan 09 Javascript
Bootstrap实现翻页效果
Nov 27 Javascript
javascript填充默认头像方法
Feb 22 Javascript
JavaScript闭包与作用域链实例分析
Jan 21 Javascript
Vue使用axios引起的后台session不同操作
Aug 14 Javascript
三步搞定:Vue.js调用Android原生操作
Sep 07 Javascript
基于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
解析PHP提交后跳转
2013/06/23 PHP
Zend Framework页面缓存实例
2014/06/25 PHP
详解PHP中的PDO类
2015/07/06 PHP
PHP读MYSQL中文乱码的快速解决方法
2016/10/01 PHP
php 多进程编程父进程的阻塞与非阻塞实例分析
2020/02/22 PHP
用js实现下载远程文件并保存在本地的脚本
2008/05/06 Javascript
动态样式类封装JS代码
2009/09/02 Javascript
jQuery实现form表单reset按钮重置清空表单功能
2012/12/18 Javascript
javascript实现标签切换代码示例
2016/05/22 Javascript
JS新包管理工具yarn和npm的对比与使用入门
2016/12/09 Javascript
无阻塞加载js,防止因js加载不了影响页面显示的问题
2016/12/18 Javascript
js仿淘宝商品放大预览功能
2017/03/15 Javascript
浅谈webpack打包过程中因为图片的路径导致的问题
2018/02/21 Javascript
vue中前进刷新、后退缓存用户浏览数据和浏览位置的实例讲解
2018/09/21 Javascript
Element-ui DatePicker显示周数的方法示例
2019/07/19 Javascript
vue 路由守卫(导航守卫)及其具体使用
2020/02/25 Javascript
python以环状形式组合排列图片并输出的方法
2015/03/17 Python
python中zip和unzip数据的方法
2015/05/27 Python
利用Pandas 创建空的DataFrame方法
2018/04/08 Python
一百多行python代码实现抢票助手
2018/09/25 Python
使用python根据端口号关闭进程的方法
2018/11/06 Python
python 调用有道api接口的方法
2019/01/03 Python
html5 兼容IE6结构的实现代码
2012/05/14 HTML / CSS
Html5元素及基本语法详解
2016/08/02 HTML / CSS
使用phonegap获取设备的一些信息方法
2017/03/31 HTML / CSS
ECHT官方网站:男女健身服
2020/02/14 全球购物
高中三年学习生活的自我评价
2013/10/10 职场文书
简短证婚人证婚词
2014/01/09 职场文书
《大江保卫战》教学反思
2014/04/11 职场文书
2015年元旦活动总结
2014/05/09 职场文书
移交协议书
2014/08/19 职场文书
费城故事观后感
2015/06/10 职场文书
python实现层次聚类的方法
2021/11/01 Python
Linux下使用C语言代码搭建一个简单的HTTP服务器
2022/04/13 Servers
springboot+rabbitmq实现智能家居实例详解
2022/07/23 Java/Android
Win11 Dev 预览版25174.1000发布 (附更新修复内容汇总)
2022/08/05 数码科技