【发布时间】:2013-12-16 19:41:06
【问题描述】:
我确定这是 JS 中的一个常见问题,我使用 JS 创建了调整大小的功能,最初这个功能只用于我网站的一个项目,但是现在我选择在多个项目上使用它。我的 JS 函数当前使用document.getElementById(id);,但是我想更改它,以便它查找类名.resize。如何才能做到这一点?
下面是我的 JS 的 sn-p
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_01239.jpg" />
<img id="resize" src="http://www.colette.fr/media/push/EGIFT_01234.jpg" />
<img id="resize" src="http://www.colette.fr/media/push/swa_mmm_001255.jpg" />
</section>
目前,调整大小功能仅适用于第一张图像,但其他图像不调整大小。在有人攻击我有关网络标准的问题之前,img 标签中的id 只是为了测试功能。
【问题讨论】:
-
你试过 document.getElementsByClassName 吗?将您所有的 img id 更改为 class 并尝试
-
@Gowsikan yup 试过了,但是我得到了一个错误,我认为就像我自己一样简单
标签: javascript html