我肯定是在JS一個共同的問題,我已經創建了調整功能使用JS,最初這個功能只是將要在我的網站的一個項目中使用,但現在我已經選擇使用它多個項目。我的JS功能目前使用document.getElementById(id);
不過,我想改變這種做法,它看起來出來的類名.resize
。如何才能做到這一點? 下面是我的JS更改的getElementById到getElementsByClassName方法
function ebi(id){
return document.getElementById(id);
}
if(ebi('resize')
.complete==true){
iscale(ebi(''));
}
ebi('resize')
.onload=function(){iscale(this);
}
function iscale(o){
//alert(o.width);
var sar=o.width/o.height;
var tar=1,tiw,tih,xoff,yoff;
if(o.width<=425&&o.height<=467){
tiw=o.width;
tih=o.height;
}else if(tar>sar){
tiw=425*sar;
tih=467;
}
else{
tiw=425;
tih=467/sar;
}
xoff=(680-tiw)>>1;
yoff=(209-tih)>>1;
//alert(xoff);
o.width=tiw;o.height=tih;
o.style.top=yoff+"px";
o.style.left=xoff+"px";
}
function $(id){return document.getElementById(id);}
HTML
<section id="homeSlide" class="shadow">
<img id="resize"class='opaque' src="http://www.colette.fr/media/push/pony_.jpg" />
<img id="resize" src="http://www.colette.fr/media/push/EGIFT_.jpg" />
<img id="resize" src="http://www.colette.fr/media/push/swa_mmm_001255.jpg" />
</section>
目前的調整大小功能僅適用於第一個圖像的片段,但是其他的圖像不會進行調整。在任何人攻擊我的web標準之前,img標籤中的id
只是爲了測試這個函數。
你試過document.getElementsByClassName?更改所有的img id到課堂上,然後嘗試 – Gowsikan
@Gowsikan yup試過,但是我得到一個錯誤,我以爲是那麼簡單,我自己 – NewKidOnTheBlock