问题描述
我怎样才能使用javascript设置元素的宽度/高度在REMS?
这就是我想要做的:
function generateProgressItems(count)
{
console.log("doing it...");
let container = document.getElementById("progress-container");
for(var i=0; i<count; ++i)
{
console.log("creating image");
let img = document.createElement('img');
img.src = 'lock_closed.png';
img.width = "100rem";
img.height ="100rem";
container.appendChild(img);
}
}
这似乎并没有被工作和宽度/高度被设定为0
解决方法
这应该有效。我添加了背景色,这样即使由于某种原因 PNG 无法加载,您也可以看到图像的空间。
function generateProgressItems(count) {
"use strict";
let container = document.getElementById("progress-container");
for (var index=0; index < count; ++index) {
let img = document.createElement("img");
img.src = "lock_closed.png";
img.style.background = "blue";
img.style.width = "2rem"; //100rem is fine too
img.style.height ="2rem"; //but it's huge.
container.appendChild(img);
}
}
generateProgressItems(5);