所以我有一個陣列,包含電影的標題和封面。我正在嘗試在彈性框中顯示名稱,使用封面影像作為背景影像。我也試著這樣寫:document.div.style.backgroundColor = "url(infoForThatDay[each]['cover'])";但它也不起作用
這是我的代碼片段:
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);
}
提前謝謝你<3祝你有美好的一天^_^
uj5u.com熱心網友回復:
div.style.background = `url("${moviesToDisplay[each]['cover']}")`;
要將影像設定為背景,設定backgroundImage或background屬性。
此外,您將需要使用字串插值,否則它將背景影像設定為字串。
另外,url()需要一個字串,這就是""需要的原因。
uj5u.com熱心網友回復:
您需要使用字串插值來獲取值。您也不能將url值與 一起使用,而background-color應使用background:
div.style.background = `url(${moviesToDisplay[each]['cover']})`;
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/494667.html
標籤:javascript html
