var temp=1;
function ss(){
    temp+=1;
    img=new Image();
    img.src="data/file/design/BZ/mt/"+temp+".jpg";var nWidth=img.width/2;
document.getElementById("ff").innerHTML=nWidth;
}
我用上面的函数来获取序列图片的宽度的一半结果但有时却为零请问是怎么回事??
特别是在服务器上几率会更多,是不是图没加载过来?怎么解决?谢谢!

解决方案 »

  1.   

    img.onload=function(){
          //Invoked when image is fully loaded
          //you can handler width here
    }
      

  2.   

    在图片的onload事件里面获取...
      

  3.   

    谢谢,img.onload应该是加载完处理事件,那么没加载完怎么表示??
      

  4.   

    Image inherits event handlers from HTMLElement and defines the following:onabortInvoked if page loading is stopped before the image is fully downloaded.onerrorInvoked if an error occurs while downloading the image.onloadInvoked when the image successfully finishes loading.image只有3这事件处理。你把你要处理的代码放onload函数里
    如果没有加载完就不会执行.等完成了再继续执行就可以了