使用变量设置图像的宽度
Set width of an image with a variable
我正在创建一个排名条形图,其中包含同一条形图的五次迭代。我想用一个实际上是百分比的变量来改变每个条的宽度。我可以通过将每个百分比作为每个图像的宽度来轻松完成。喜欢....
<!DOCTYPE html>
<html>
<body>
<table width="100%" cellspacing="0" cellpadding="0" >
<tbody>
<tr>
<td width="20%" >5 stars</td>
<td width="80%" bgcolor="#e2e2e2">
<img src="http://qr8.us/images/review-bar.jpg" width=80% height="10" alt=""/>
</td>
</tr>
<tr>
<td >4 stars</td>
<td bgcolor="#e2e2e2">
<img src="http://qr8.us/images/review-bar.jpg" width=14% height="10" alt=""/>
</td>
</tr>
<tr>
<td >3 stars</td>
<td bgcolor="#e2e2e2">
<img src="http://qr8.us/images/review-bar.jpg" width=3% height="10" alt=""/>
</td>
</tr>
<tr>
<td >2 stars</td>
<td bgcolor="#e2e2e2">
<img src="http://qr8.us/images/review-bar.jpg" width=2% height="10" alt=""/>
</td>
</tr>
<tr>
<td >1 star</td>
<td bgcolor="#e2e2e2">
<img src="http://qr8.us/images/review-bar.jpg" width=1% height="10" alt=""/>
</td>
</tr>
</tbody>
</table>
</body>
</html>
但是,我想将该宽度作为我在别处计算的变量的百分比应用。变量看起来像 ...
var ttlrevsprcntfive = 80;
var ttlrevsprcntfour = 14;
var ttlrevsprcntthree = 3;
var ttlrevsprcnttwo = 2;
var ttlrevsprcntone = 1;
我已尽一切努力使变量在每个图像的宽度属性中起作用。极大的失败。
如何将每个变量的值分配给图像宽度?记住是百分比。谢谢
您究竟是如何尝试应用该值的?
你可以做到这一点,例如通过这样做:
YourImageObject.style.width = ttlrevsprcntfive + "%";
您可以获得 YourImageObject 例如通过 document.getElementsByTagName("img")[imageIndex] 或通过 document.getElementById('imageId') 或使用JQuery.
这可能就是您要找的。
工作 fiddle here
'use-strict';
(function(){
function resize() {
var img = document.getElementsByTagName('img');
for(var i=0;i<img.length;i++) {
img[i].style.width = 20 + '%';
img[i].style.height = 20 + '%';
}
}
}());
关于您的评论,我已经用您的 html 源代码做了一个例子。查看更新后的 fiddle here
这应该可以完成工作。如果您满意,请接受此解决方案作为正确答案并单击左侧的箭头指向post。
我正在创建一个排名条形图,其中包含同一条形图的五次迭代。我想用一个实际上是百分比的变量来改变每个条的宽度。我可以通过将每个百分比作为每个图像的宽度来轻松完成。喜欢....
<!DOCTYPE html>
<html>
<body>
<table width="100%" cellspacing="0" cellpadding="0" >
<tbody>
<tr>
<td width="20%" >5 stars</td>
<td width="80%" bgcolor="#e2e2e2">
<img src="http://qr8.us/images/review-bar.jpg" width=80% height="10" alt=""/>
</td>
</tr>
<tr>
<td >4 stars</td>
<td bgcolor="#e2e2e2">
<img src="http://qr8.us/images/review-bar.jpg" width=14% height="10" alt=""/>
</td>
</tr>
<tr>
<td >3 stars</td>
<td bgcolor="#e2e2e2">
<img src="http://qr8.us/images/review-bar.jpg" width=3% height="10" alt=""/>
</td>
</tr>
<tr>
<td >2 stars</td>
<td bgcolor="#e2e2e2">
<img src="http://qr8.us/images/review-bar.jpg" width=2% height="10" alt=""/>
</td>
</tr>
<tr>
<td >1 star</td>
<td bgcolor="#e2e2e2">
<img src="http://qr8.us/images/review-bar.jpg" width=1% height="10" alt=""/>
</td>
</tr>
</tbody>
</table>
</body>
</html>
但是,我想将该宽度作为我在别处计算的变量的百分比应用。变量看起来像 ...
var ttlrevsprcntfive = 80;
var ttlrevsprcntfour = 14;
var ttlrevsprcntthree = 3;
var ttlrevsprcnttwo = 2;
var ttlrevsprcntone = 1;
我已尽一切努力使变量在每个图像的宽度属性中起作用。极大的失败。
如何将每个变量的值分配给图像宽度?记住是百分比。谢谢
您究竟是如何尝试应用该值的?
你可以做到这一点,例如通过这样做:
YourImageObject.style.width = ttlrevsprcntfive + "%";
您可以获得 YourImageObject 例如通过 document.getElementsByTagName("img")[imageIndex] 或通过 document.getElementById('imageId') 或使用JQuery.
这可能就是您要找的。 工作 fiddle here
'use-strict';
(function(){
function resize() {
var img = document.getElementsByTagName('img');
for(var i=0;i<img.length;i++) {
img[i].style.width = 20 + '%';
img[i].style.height = 20 + '%';
}
}
}());
关于您的评论,我已经用您的 html 源代码做了一个例子。查看更新后的 fiddle here 这应该可以完成工作。如果您满意,请接受此解决方案作为正确答案并单击左侧的箭头指向post。