2 回答

TA貢獻1817條經驗 獲得超6個贊
嘗試這個,
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貢獻1880條經驗 獲得超4個贊
您也可以直接使用樣式對象。但對于 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 關注
- 110 瀏覽
添加回答
舉報