Home >Web Front-end >JS Tutorial >jquery click li to prevent repeated loading implementation code_jquery

jquery click li to prevent repeated loading implementation code_jquery

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

Because loading content is slow, the user may inadvertently click twice on the li, which will result in two requests. This is what we don't want to see.
Today, two demos were sent to me in the javascript-jquery group. His method was to copy the clicked li node first, and then
insert it back again after loading, which is obviously not suitable. I made the function.
I was at a loss, so I thought about it, why not add the loading image when li is clicked (before ajax loads), and determine whether the img element exists on the li node.
If not, load the ajax content, otherwise it will not be processed.
The test passed, (^o^)/.

Copy code The code is as follows:





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'/>"); //加载图片 <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