jQuery ajax请求和图像加载
我跟随ajax调用:
$("#container").html("loading..."); $.ajax({ url: "somedoc.php", type: "POST", dataType: "html", success: function(response){ if(response != ''){ $("#container").html(response); } } });
响应如下:
在将所有图像下载到用户之前,Ajax调用结束。 那么是否可以在此处使用加载function并显示加载文本而不是所有图像都已加载?
你的帮助会被贬低。
您可以将加载事件附加到图像,看看这是否适合您:
$.ajax({ url: "somedoc.php", type: "POST", dataType: "html", success: function(response){ if(response != ''){ var $imgs = $("#container").html(response) .find("img") //find the appended images .hide(), //hide them imgAmount = $imgs.length, imgCounter = 0; //Here you should show your loading text $imgs.on("load", function(){ imgCounter++; if(imgCounter === imgAmount) { //Here you should hide your loading text $imgs.fadeIn(); //show the images when they're loaded } }); } } });
是的,如果你知道你应该得到多少图像(你可以算上李的)。
#myWrapperelement
=您注入响应的元素。
关于成功:function:
$("#MyloadingLabel").show() var num=$("ul li").length; $("#myWrapperelement").on('load',"img ",function (){ num--; if (num==0) $("#MyloadingLabel").hide(); });