忘了说了,是有的时候等于0,有的时候是正常的,我也是在不经意之时发现不对头,然后就加了
if(ImgD.height==0)
        alert("高:"+image.height+"  宽:"+image.width+"  字:"+ImgD.vspace );
这个语句,结果发现,它让执行了。

解决方案 »

  1.   

    你得确保ImgD是否加载完成。ImgD都还没下载完哪来的宽高?
      

  2.   

    楼上正解为什么要new一个Image对象?在图片onload的时候直接判断就可以了
      

  3.   

    function adjustsize(ImgD)
    {
    if(ImgD.width >0 && ImgD.height>0)
    {
          if(ImgD.width>ImgD.height)
      {
                 var temp1=ImgD.width/160;
                 var temp2=ImgD.height;
         ImgD.width=160;
         ImgD.height=temp2/temp1;
         ImgD.vspace=ImgD.vspace+(160-ImgD.height)/2;
      }
     else
     {
         var temp1=ImgD.height/160;
         var temp2=ImgD.width;
         ImgD.height=160;
     ImgD.width=temp2/temp1;  
     }
     ImgD.style.visibility="visible";
    }
    }还是不行啊,有的时候一个图片也显示不出来
      

  4.   

    发现错误原因还是ImgD.width =0 怎么保证保ImgD加载完成啊
      

  5.   

    给ImgD添加一个Onload事件。下载完成会触发事件。
      

  6.   

    <img src="abc.jpg" onload="adjustsize(this)" style="display:none">
      

  7.   

    function adjustsize(ImgD)
    {
    if(ImgD.width >0 && ImgD.height>0)
    {
          if(ImgD.width>ImgD.height)
      {
                 var temp1=ImgD.width/160;
                 var temp2=ImgD.height;
         ImgD.width=160;
         ImgD.height=temp2/temp1;
         ImgD.vspace=ImgD.vspace+(160-ImgD.height)/2;
      }
     else
     {
         var temp1=ImgD.height/160;
         var temp2=ImgD.width;
         ImgD.height=160;
     ImgD.width=temp2/temp1;  
     }
     ImgD.style.visibility="visible";
    }
            else
            {
                 alert(ImgD.width+","+ImgD.height);
            }
    }<td width="200"  align="center"><img style="visibility:hidden" src=photo/13.jpg  vspace=10 border=0  onload="adjustsize(this)"> <br>
      

  8.   

    你试着alert(image.height);看看它的值是什么
      

  9.   

    可能你的图片格式不对。并非所有jpg都是ie支持的。你直接把图片拉进一个ie窗口,右键看一下能否看到尺寸。
      

  10.   

    我的图片是JPG的啊,
    “你直接把图片拉进一个ie窗口,右键看一下能否看到尺寸。”
    这个什么意思,我一拉到IE窗口就显示出来了 没有尺寸啊
      

  11.   

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>JK:支持民族工业,尽量少买X货</title>
    <script>
    function setMaxHeight(imgObj,maxHeight){
    if(imgObj.offsetHeight>maxHeight) {
    imgObj.height=maxHeight;
    imgObj.removeAttribute('width');
    }
    }
    </script>
    </head>
    <body>
    <div style="font-size:10pt;">
    注1:设定图片的最大宽度或高度,同时保证图片比例。 <br/>         <br/>  
    注:本页面仅在IE6/FireFox1.5下测试过。其它浏览器或其它版本未经测试。<br/>         
    注-----:JK:<a href="mailTo:[email protected]?subject=About IMG">[email protected]</a><br/>      
    <hr/>  
    </div><IMG id="big_img2"
    src="http://img.phoenixtv.com/tres/recommend/phoenixtv/scnews/070828-news-950x90.gif" width="200" onload="setMaxHeight(this,300);">
    </body>
    </html>
      

  12.   

    第一次学做WEB,什么都没见过啊。。不过问题还没解决啊回 JK_10000(JK) ( ) 信誉:121你的代码我试了不行,function adjustsize(ImgD)
    {
    if(ImgD.offsetHeight>160)
    {
       ImgD.width=ImgD.width*160/ImgD.height;
       ImgD.height=160;
       imgObj.removeAttribute("width");
    }
    else
        {
            if(ImgD.height==0)
            {
                alert(ImgD.offsetHeight);
            }
            else
                ImgD.vspace=(160-ImgD.height)/2;
        }
    }还是会弹出 0 的对话框,怎么才能保证adjustsize调用时图像已经加载上了,如果不能怎么来解决这个问题啊,如果问题低级还请大虾们见谅。
      

  13.   

    还有 imgObj.removeAttribute('width');这句话没作用啊,我的是IE6
      

  14.   

    问题完美解决,有兴趣可见下面两帖。
    http://community.csdn.net/Expert/topic/5734/5734245.xml?temp=.8744165
    http://community.csdn.net/Expert/topic/5736/5736479.xml?temp=.6601526