用变量设置图像的宽度
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;
我已经尝试了一切让变量在每个图像的宽度属性工作。大失败。
如何将每个变量的值赋给图像宽度?记住这是一个百分比。由于
这可能是您正在寻找的。在这里工作
'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源代码做了一个例子。在这里查看更新的小提琴这样就可以了。如果你满意,请接受这个解决方案作为正确的答案,并点击左边的箭头到帖子。
您究竟是如何尝试应用该值的?
你可以这样做:
YourImageObject.style。
你可以得到YourImageObject例如document.getElementsByTagName("img")[imageIndex]或document.getElementById('imageId')或使用JQuery。
相关文章:
- 使用带有变量的javascript更改图像src
- 如何使按钮图像在单击时添加到变量中
- Javascript:如何使变量中的图像可点击
- 用于基于 PHP 变量更改图像的 JavaScript
- LazyLoad带有变量的Jekyll图像
- 如何将图像文件以及JavaScript变量的值上传到服务器和数据库
- HTML图像布局使用PHP变量,该变量是用JavaScript动态设置的〔o r…〕
- 更新图像切换上的php会话变量
- 有没有一种存储图像变量的短方法
- 将生成的随机数存储在变量中,然后将该变量作为文件名调用以显示图像.Javascript
- Javascript - 使用变量更改图像
- 将变量 javascript 添加到用于旋转图像的链接中
- 如何使用在 javascript 中分配给图像的变量,并使用 switch 语句在 html 中显示它们
- 在图像网址中传递变量
- 如何将“Flexslider”当前图像编号获取到可以在函数中使用的变量中
- ERB/Coffee Rails 中图像网址的未定义局部变量或方法“image”
- 如何将
标记 SRC 加载到 javascript 变量中,以便在多个图像中重复使用
- 移动图像 ..JavaScript 将变量声明为字符串
- 在 javascript 中设置变量中图像的高度和宽度
- 添加到图像变量的链接