日期:2014-05-18  浏览次数:20608 次

JS问题,JS区没搞定,跑这来求救
一个改变图像大小的JS,老出问题,adjustsize这个函数是onLoad时才调用的,可调用的时候它的宽和高竟然都是0,怎么办啊。下面的代码可以弹出“0,0”的对话框。

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/>


此问题在JS区的地址是
http://community.csdn.net/Expert/topic/5733/5733109.xml?temp=.2805445
解决的哥们顺便去领一下分。

------解决方案--------------------
onLoad的时候可能图片还没的Load进来,当然是0,0了。
你可以先创建一个Image(在onload之前),把图片load进来再说。

var img = new Image();
img.src= "图片地址 ";
这个你可以参考一下:http://news.lnwest.com/program/article.asp?id=471

要复杂一点的可以看看:
http://bbs.51js.com/viewthread.php?tid=14837&highlight=


这样操作,你也不需要用隐藏的IMG标签了
------解决方案--------------------
不要在onload事件里调用你的js
直接把adjustsize函数里面的代码写在页面 </html> 的后面
------解决方案--------------------
<td width= "200 " align= "center "> <img style= "visibility:hidden " src=photo/13.jpg vspace=10 border=0 id= "image1 "> <br/>


.....


adjustsize(document.all( "image1 "));
</body>

</html>