1.可以用 document.getElementsByTagName("img")获取到所有的img对象
2.在遍历每一个对象,来指定它的宽和高

解决方案 »

  1.   

    <script language="javascript"> 
    var flag=false; 
    function DrawImage(ImgD){ 
    var image=new Image(); 
    image.src=ImgD.src; 
    if(image.width>0 && image.height>0){ 
    flag=true; 
    if(image.width/image.height>=290/240){ 
    if(image.width>290){ 
    ImgD.width=290; 
    ImgD.height=(image.height*290)/image.width; 
    }else{ 
    ImgD.width=image.width; 
    ImgD.height=image.height; 


    else{ 
    if(image.height>280){ 
    ImgD.height=280; 
    ImgD.width=(image.width*280)/image.height; 
    }else{ 
    ImgD.width=image.width; 
    ImgD.height=image.height; 




    function Init() {
    var obj=document.getElementsByTagName("img");
    for (var j=0; j < obj.length; j++) {
    DrawImage(obj[j]);
    }
    }
    window.onload=Init;
    </script> 
    <body>
    <img src="../images/ybfq_1.jpg" alt="0" width="500" height="261" />
    <img src="../images/ybfq_2.jpg" alt="0" width="757" height="234" />不用理会$str里的.直接读取所以图片.
      

  2.   

    php也可以处理图片自适应的啊~
      

  3.   

    楼上的都答完了。用document.getElementsByTagName("img")遍历是正解。不用动$str.
      

  4.   


    引用:
    楼上的都答完了。用document.getElementsByTagName("img")遍历是正解。不用动$str.如果页面中有很多图片,但只有从$str里出来的图片才想自适应,则可对$str里的<img />内容加点特殊内容,当然这样做的话就可以直接来处理$str就行了,不在浏览器端动用JS了