jQuery检查图像是否已加载

时间:2011-03-23 11:12:18

标签: javascript jquery image loaded

我需要以下代码的帮助:

var timeoutID=0;
var currentImage=0;//first image is position 0 in arrImages array
var arrImages=[bla bla bla array of image URLs];
function slideShow()
{
   if($('#myImg').complete)//   <------- Here is where it fails as that's UNDEFINED.
   {
      //curentImage is a global var that remebembers the on-screen image array key
      var nextImage=currentImage+1;

      //arrImages is the array of image URLs
      if(nextImage>=arrImages.length){nextImage=0;}

      $('#myImg').attr('src',nextImage);

      clearTimeout(timeoutID);
      //Change image each second after previous image was loaded
      timeoutID=setTimeout("slideShow()",1000);
   }
   else
   {
      $('#myImg').load(slideShow);
   }
}

基本上我想每秒更改#myImg的src,前提是计数器在加载图像后开始。

* 我讨厌Stack Overflow文本编辑器中的代码按钮!

2 个答案:

答案 0 :(得分:16)

'complete'适用于原生javascript对象。所以你应该从jQuery获取javascript对象。 等,

$('#myImg')[0].complete

$('#myImg').get(0).complete

答案 1 :(得分:2)

如果我没记错,图像会在加载时获得宽度和高度。所以我想你可以检查图像的宽度。只要该值为0,就不会加载图像。