2 回答

TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超6個(gè)贊
嘗試這個(gè),
document.getElementById("Your div ID").style.backgroundImage = "url('https://firebasestorage.googleapis.com/v0/b/fir-firebase-182ca.appspot.com/o/images%2FScreenshot%20(2).png?alt=media&token=0edf4c43-e791-48b1-b2f4-b8ec5e65e296')";

TA貢獻(xiàn)1880條經(jīng)驗(yàn) 獲得超4個(gè)贊
您也可以直接使用樣式對(duì)象。但對(duì)于 css 背景,您需要將圖像 url 放在“url('')”中
function myFunction() {
var imagePath = "https://firebasestorage.googleapis.com/v0/b/fir-firebase-182ca.appspot.com/o/images%2FScreenshot%20(2).png?alt=media&token=0edf4c43-e791-48b1-b2f4-b8ec5e65e296";
var icon = document.createElement('img');
icon.style.backgroundColor = "red";
//icon.src = imagePath ;
icon.style.backgroundImage = "url('" + imagePath + "')";
var parent = document.getElementById("loggedDisplay");
parent.appendChild(icon);
}
#loggedDisplay img {
display: block;
width: 100%;
height: 300px;
}
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Try it</button>
<div id="loggedDisplay"></div>
</body>
</html>
- 2 回答
- 0 關(guān)注
- 107 瀏覽
添加回答
舉報(bào)