没用过你这种方法创建图片。我都是直接用
document.creatElement("IMG")
或者
document.write("<img></img>")
的方式

解决方案 »

  1.   

    “通过alert()来查看每次返回图片的宽度值,可有时候返回的值是0。”
    ==》因为图片还没有加载完毕,所以返回值为0
    可以先通过readystate判断图片是否已经加载完成,然后再取width;
    <html>
    <head>
    <meta http-equiv="Expires" content="0">
    <meta http-equiv="Cache-Control" content="no-store">
    <meta http-equiv="Pragma" content="no-cache">
    </head>
    <body>
    <script language="javascript">
    var aImg=new Image();
    var photo_width,photo_height;
    var srcPath="http://images.csdn.net/upimgs/20060124_760_90.jpg"
    aImg.src=srcPath;
    function getWidth(){
      if (aImg.readyState=="complete"){
    if(aImg.width>600){
    photo_width=600;
    photo_height=aImg.height*(600/aImg.width);
    }else{
    photo_width=aImg.width;
    photo_height=aImg.height;
    }
    //alert ("photo_width="+photo_width+"\nphoto_height="+photo_height)
    document.write("<a href='"+srcPath+"' target='_blank'><img border='0' src='"+srcPath+"' height='"+photo_height+"' width='"+photo_width+"'></a>");
    }else setTimeout("getWidth()","100");
    }
    getWidth();
    </script>
    </body>
    </html>
      

  2.   

    请问readyState属性在非IE的浏览器中被支持吗?