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

因为加载内容比较慢,所以用户可能在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.jb51.net/js/jquery_li_click/demo.html
打包下载 http://xiazai.jb51.net/201012/yuanma/jquery_li_click.rar

时间: 2024-09-01 12:40:10

jquery 单击li防止重复加载的实现代码_jquery的相关文章

基于jQuery的公告无限循环滚动实现代码_jquery

在线演示:http://demo.jb51.net/js/2012/callboard/jQuery代码 复制代码 代码如下: //第二版:Newton改造 (function (win){ var callboarTimer; var callboard = $('#callboard'); var callboardUl = callboard.find('ul'); var callboardLi = callboard.find('li'); var liLen = callboard.

基于jQuery实现仿51job城市选择功能实例代码_jquery

前些文章用写过,省市县三级联动,但是感觉选择的时候不够直观,现在改进了下,效果如下图 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="City.aspx.cs" Inherits="System_Select_City" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transition

jQuery+css实现的切换图片功能代码_jquery

本文实例讲述了jQuery+css实现的切换图片功能代码.分享给大家供大家参考,具体如下: 运行效果截图如下: 具体代码如下: <!DOCTYPE html> <html> <head> <title>demo</title> <script type="text/javascript" src="jquery.js"></script> <style type="t

jQuery实现网页顶部固定导航效果代码_jquery

本文实例讲述了jQuery实现网页顶部固定导航效果代码.分享给大家供大家参考,具体如下: 运行效果截图如下: 具体代码如下: <!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/

jquery单击事件和双击事件冲突解决方案_jquery

本人需要给bootstrap-treeview的树节点添加双击事件.而该插件原生方法中不带双击事件功能.该插件的节点默认绑定的单击事件,由此引起了单击事件和双击事件的冲突. 编写测试代码 引起冲突的代码: 问题效果展示: 每一次触发双击事件都会引起两次单击事件 解决冲突的代码: 解决问题效果展示: 完美解决单击事件和双击事件冲突问题 这里主要用到两个HTMLDOMWindow对象中函数,settimeout(),clearTimeout() 我这里两个单击事件触发的时间间隔设置在等于300毫秒,

jquery实现的简单二级菜单效果代码_jquery

本文实例讲述了jquery实现的简单二级菜单效果代码.分享给大家供大家参考.具体如下: 这是一款基于jquery实现的二级菜单,研究了这么多天,才学会用jquery写一个菜单,也算是对自己的鼓励吧,自我感觉这个菜单很不错,欢迎朋友们指点江山. 运行效果截图如下: 在线演示地址如下: http://demo.jb51.net/js/2015/jquery-simple-2l-menu-style-demo-codes/ 具体代码如下: <!DOCTYPE html PUBLIC "-//W3

jQuery渐变发光导航菜单的实例代码_jquery

下面和大家分享一下具体的实现过程. HTML标签结构: 复制代码 代码如下: <ul class="animation_menu">     <li class="current">         <a href="#">菜单一<span>菜单一</span></a>     </li>     <li>         <a href=&qu

jquery实现超简洁的TAB选项卡效果代码_jquery

本文实例讲述了jquery实现超简洁的TAB选项卡效果代码.分享给大家供大家参考.具体如下: 这是一款超简洁的TAB选项卡,个人觉得有点太简单,简单的有点不习惯,美化修饰一下其实效果更好.实际上应该为点击过的TAB添加一个背景色,区分一下,这样比较好. 先来看看运行效果截图: 在线演示地址如下: http://demo.jb51.net/js/2015/jquery-easy-tab-cha-menu-codes/ 具体代码如下: <!DOCTYPE html PUBLIC "-//W3C

jQuery实现的网页竖向菜单效果代码_jquery

本文实例讲述了jQuery实现的网页竖向菜单效果代码.分享给大家供大家参考.具体如下: 这是一款基于jQuery实现竖向的网页菜单代码,可折叠展开的二级网页菜单,修改一下可用在后台管理中,显示在左侧的那种管理菜单.jquery加入后方便实现了菜单展开和合拢的功能,还加入了少许动画效果,兼容性好. 运行效果截图如下: 在线演示地址如下: http://demo.jb51.net/js/2015/jquery-v-web-menu-style-codes/ 具体代码如下: <!DOCTYPE htm