This time I will bring you JQuery to call Ajax to load images. What are the precautions for JQuery to call Ajax to load images? The following is a practical case, let's take a look.
The first idea that comes to mind is to use cache, that is, first display the prompt message, then get the picture, call back when the get is completed, and change the src of theimg tag, because it just got However, there is a cache, so the image will be displayed immediately.
Page elements:<input class="picbtn" type="button" value="Next" /> <p class="tip">正在加载……</p> <p class="notice"> <img /> </p>
$(".picbtn").click(function(){NextPic();});
$(".tip").slideDown(200); //显示提示 $.get(PicArr[CurrPic],function(){ $("img").attr("src",PicArr[CurrPic]); $(".tip").slideUp(200); CurrPic++; if(CurrPic>4) CurrPic=0; });
$("img").attr("src",PicArr[CurrPic]) .bind('load',function(){$(".tip").slideUp(200);CurrPic++;if(CurrPic>4)CurrPic=0;});
callback functions will be too many runs.
I thought it might be a problem with event binding, because the binding of onclick event is$(Element).bind("click",callback)
$(Element).click(callback)
<!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=gb2312" /> <title>JQUERY动态加载图片</title> <script type="text/javascript" src="jquery/jquery-1.3.2.min.js"></script> <script type="text/javascript"> (function($){ $.NextPic=function() { $(".tip").slideDown(200); $("img").attr("src",PicArr[CurrPic]).one('load',function(){$(".tip").slideUp(200);CurrPic++;if(CurrPic>4)CurrPic=0;}); //$("img").load(PicArr[CurrPic],function(){$(this).attr("src",PicArr[CurrPic]);$(".tip").slideUp(200);alert(CurrPic);CurrPic++;if(CurrPic>4)CurrPic=0;}); } })(jQuery); $(document).ready(function(){ PicArr = new Array("1.jpg","2.jpg","3.jpg","4.jpg","5.jpg"); CurrPic=0; $(".tip").css({"position":"absolute","top":"100px","left":"50px"}); $(".tip").hide(); $(".scoll").click(function(){$.NextPic();}); }) </script> </head> <body> <input class="picbtn" type="button" value="Next" /> <p class="tip">正在加载……</p> <p class="notice"> <img id="img"/> </p> </body> </html>
Detailed explanation of jQuery AJAX implementation calling background steps
The above is the detailed content of JQuery calls Ajax to load images. For more information, please follow other related articles on the PHP Chinese website!