首页 > web前端 > js教程 > 正文

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

php中文网
发布: 2016-05-16 18:13:33
原创
1518人浏览过

因为加载内容比较慢,所以用户可能在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
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号