【发布时间】:2022-06-23 00:03:40
【问题描述】:
所以我有一个数组,包含电影的标题和封面。我正在尝试在弹性框中显示名称,使用封面图像作为背景图像。
我也试过这样写:document.div.style.backgroundColor = "url(infoForThatDay[each]['cover'])"; 但也没用
这是我的代码的 sn-p:
for(let each in moviesToDisplay) {
let div = document.createElement("div")
div.innerText = moviesToDisplay[each]['title'];
//That is where I'm trying to set the background color
div.style.backgroundColor = "url(moviesToDisplay[each]['cover'])";
flex.appendChild(div);
}
提前谢谢你
【问题讨论】:
-
删除
"url(infoForThatDay[each]['cover'])"周围的引号,以便计算表达式,而不是直接用作背景颜色。 -
这是不正确的@ScottMarcus,如果是图像值,您仍然需要
url()。 -
@bryce 是的,但您不希望在动态部分周围加上引号。
标签: javascript html