jQuery:如何检查图像是否已在ajax中完成加载

我需要一些关于如何解决这个问题的建议。

在我的页面中单击LInk时,我有一个Ajax代码要执行。 ajax代码是调用PHP代码来查询某个目录中的图像(缩略图大小)文件名。 一旦执行,ajax数据将在该目录中包含我的缩略图的文件名,并动态显示我页面中的缩略图。

我想知道如何在不使用SetTimeOut()的情况下检查所有缩略图是否已经完成加载的任何建议? 加载所有缩略图后执行一些jquery代码。 我需要加载缩略图,以便我可以设置缩略图div的大小以便滚动。

方法:

function imagesLoaded(imgAr, callback){ var // This is a copy of the array of images imageAr = imgAr.slice(0), // This holds the javascript Image Objects images = [], // This is the number of Images that has loaded loadedCount = 0; var imageLoaded = function(){ // An image has finished loading, so increment the number of images loaded by 1 loadedCount++; if(loadedCount>=imageAr.length){ // If all images have finished loading, run the 'callback' function, to report back that images have all loaded callback.call(); } } // Loop around each image url for(var i=0;i 

使用:

 var imageAr = ['a.jpg', 'b.jpg', 'c.jpg']; imagesLoaded(imageAr, function(){ alert('Images loaded!'); }); 

您可以对图像使用加载方法,例如:

 $('someElement img').load(function(){ //all loaded }); 

你的意思是那样的吗?