Home  >  Article  >  Web Front-end  >  jquery 单击li防止重复加载的实现代码_jquery

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

WBOY
WBOYOriginal
2016-05-16 18:13:331127browse

因为加载内容比较慢,所以用户可能在li上不经意点击了两次,那么就会请求两次,这是我们不想看到的。
今天在javascript-jquery群上一筒子发了两个demo给我,他的方法是先将单击的li节点拷贝出来,在加载完后
在重新插回去,显然不太适合我做的功能。
正一筹莫展,想了一下,何不在li点击时加入loading图片(在ajax加载前),判断li节点上是否存在了img元素,
没有则加载ajax内容,否则不处理。
测试了可以通过,(^o^)/。

复制代码 代码如下:





jquery ajax加载绑定事件


<script> <BR>$(function(){ <BR>setHeight("#siderbar",130); // 设置侧边栏的高度 <BR>$(window).resize(function(){setHeight("#siderbar",130)}); //窗口变化时重新设置侧边栏高度 <BR>$.get("sider.html",function(data){ <BR>$('#siderbar_box').append(data); <BR>}); <BR>/*设置ID的高度*/ <BR>function setHeight(id,h){ <BR>var windowHeight=$(window).height(); <BR>$(id).css({"height":(windowHeight-h)+"px"}); <BR>} <BR>// 绑定加载后的li的查看 <BR>$("#siderbar_box ul li a").live("click",function(){ <BR>var $current=$(this); <BR>var $currentli=$(this).parent(); <BR>if($currentli.children("ul").attr("id")==undefined) //第一次需ajax加载 <BR>{ <BR>$currentli.siblings().children("ul").slideUp('fast'); <BR>$currentli.siblings().children("a").removeClass("focus"); <BR>$.get("chapter.html",function(data){ <BR>$current.addClass("focus").parent().append(data); <BR>showChapter(); //在content去显示主要内容 <BR>}); <br><br>}else{ <BR>$currentli.siblings().children("ul").slideUp('fast'); <BR>$currentli.siblings().children("a").removeClass("focus"); <BR>if($currentli.children("ul").is(":visible")) //处于展开状态 <BR>{ <br><br>$current.removeClass("focus").parent().children("ul").slideUp('fast'); <BR>}else{ <br><br>$current.addClass("focus").parent().children("ul").slideDown('slow'); <BR>showChapter(); <BR>} <BR>} <BR>}); <BR>//content显示列表标题 <BR>function showChapter(){ <BR>$.get("chapter.html",function(data){ <BR>$("#content").html(data); <BR>$("#content ul li").live("click",function(){ //绑定加载后的标题单击事件 <BR>$current=$(this); <BR>if($current.children("table").attr("id")==undefined) //单击标题,第一次ajax加载 <BR>{ <BR>if($current.children("span").find("img").size()<1) //只加载一次内容,防止多次请求加载 <BR>{ <BR>$current.children("span").append("<img src='loading.gif' border='none'/ alt="jquery 单击li防止重复加载的实现代码_jquery" >"); //加载图片 <BR>$.get("table.html",function(data){ <BR>$current.append(data).children("span").addClass("highlight").find("img").remove(); //加载完成移除加载图片 <BR>}); <BR>} <BR>}else{ <br><br>if($current.children("table").is(":visible")) <BR>{ <BR>$current.children("span").removeClass("highlight").next("table").hide(); <br><br>}else{ <br><br>$current.children("span").addClass("highlight").next("table").show(); <BR>} <BR>} <br><br>}); <BR>}); <BR>} <BR>}); <BR></script>





课程












演示地址 http://demo.jb51.net/js/jquery_li_click/demo.html
打包下载 http://xiazai.jb51.net/201012/yuanma/jquery_li_click.rar
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn