谢谢,顶也有分的,注意是 html页面.我也估计是浏览器的缓存,但不知道如何解决.

解决方案 »

  1.   

    <html>
    <head>
    <script language="javascript">
    var imgObj;
    function changeImg1(dfd){
    alert(dfd);
    }
    function changeImg(imgAdr) {
                     imgObj=document.getElementById('Image1');
                     imgObj.removeAttribute("width");
            imgObj.removeAttribute("height");
                     imgObj.style.display="";
                     imgObj.src=imgAdr;           
         if(imgObj.width<172&&imgObj.height<400)                       
           { 
             return;
           }
        else
          {
             if(imgObj.width/172>imgObj.height/400)
               {
                    imgObj.width =172; 
                    imgObj.height =imgObj.height * 172/imgObj.width;
               }
             else
               {
                    imgObj.width =imgObj.width * 400 / imgObj.height; 
                    imgObj.height =400;   
                }
    }
    }
    </script>
    </head>
    <body>
     <input id="uploadfile1" type="file"  onChange="changeImg(uploadfile1.value)">
      <img id="Image1" style="display:none">
    </body>
    </html>
      

  2.   

    把设置高度宽度在img的onload里执行如何 function changeImg(imgAdr) {
                     ...
                     imgObj.style.display="";
                     imgObj.src=imgAdr;           
             }
      function loadImg(imgObj){
         if(imgObj.width<172&&imgObj.height<400)                       
           { alert()
             return;
           }
        else
          {
             if(imgObj.width/172>imgObj.height/400)
             ...<img id="Image1" style="display:none" onload="loadImg(this)">
      

  3.   

    你试着断点调试下吧:<html>
    <head>
    <script language="javascript">function changeImg(imgAdr) 
    {
    var imgObj=document.getElementById('Image1');
    imgObj.removeAttribute("width");
    imgObj.removeAttribute("height");
    imgObj.style.display = "";
    imgObj.src = imgAdr;
    if(imgObj.width<172&&imgObj.height<400)
    {
    return false;
    }
    else
    {
    if(imgObj.width/172>imgObj.height/400)
    {
    alert('满足这条件!');
    imgObj.width = 172;
    imgObj.height = imgObj.height * 172/imgObj.width;
    }
    else
    {
    imgObj.width = imgObj.width * 400 / imgObj.height;
    imgObj.height = 400;
    }
    }

    }</script>
    </head>
    <body>
    <input id="uploadfile1" type="file" onchange="changeImg(uploadfile1.value)">
    <img id="Image1" style="display:none">
    </body>
    </html>
      

  4.   

    取值的时候用 imgObj.offsetWidth 赋值用 imgObj.width
    因为 <img> 的宽度值不一定是图片实际的宽度,高度同
      

  5.   

    fosjos(无聊的菜鸟程序员) ( ) 信誉:100    Blog 
    ----------------------------------------------------------
    你的测试过,正确,我测试下 梅老大的,马上结贴!谢谢各位
      

  6.   

    <script language="javascript">
    var imgObj;
    function changeImg1(dfd){
    alert(dfd);
    }
    function changeImg(imgAdr) {
                     imgObj=document.getElementById('Image1');
                     //imgObj.removeAttribute("width");
              //imgObj.removeAttribute("height");
                     imgObj.style.display="";
                     imgObj.src=imgAdr;  
         if(imgObj.width<172&&imgObj.height<400)                       
           { 
             return;
           }
        else
          {
             if(imgObj.width/172>imgObj.height/400)
               {
                    imgObj.width =172; 
                    imgObj.height =imgObj.height * 172/imgObj.width;
               }
             else
               {
                    imgObj.width =imgObj.width * 400 / imgObj.height; 
                    imgObj.height =400;   
                }
    }
    }
    </script>这样试了一下,不知道是否符合楼主的意思O.0
      

  7.   


    在我这里是能下常运行的呀这样操作:先选择一张符合条件的图片a.jpg,然后再选择一张不符合的b.jpg;如此的循环,a>b>a>b...;发现有时候选择b(不符合)的时候变成符合了. 没有按比率缩小.没有你说的这种现象应该是浏览器的问题,待解决