誰可以給我解釋一下這個:
我有一個背景影像正在作業的div:
<section class="bottom-banner" id="imagebannerthree" style="background-image:url(https://*****.com.au/wp-content/custom-products/1628/bottom-3.jpg);">
</section>
但是當我嘗試使用 javascript 加載背景影像時,沒有加載影像:
document.getElementById('imagebannerthree').style.backgroundImage = checkImage("https://*****.com.au/wp-content/custom-products/1628/bottom-3.jpg");
function checkImage(url) {
var http = new XMLHttpRequest();
http.open('HEAD', url, false);
http.send();
if (http.status == 200) {
return 'url(' url ')';
} else {
document.getElementById('' parentdiv).remove();
}
}
uj5u.com熱心網友回復:
您需要在為 style.backgroudImage 賦值時使用引號
document.getElementById('imagebannerthree').style.backgroundImage = "url('https://*****.com.au/wp-content/custom-products/1628/bottom-3.jpg')";
您很可能會收到錯誤“url 不是函式”。
uj5u.com熱心網友回復:
document.getElementById('imagebanner').style.backgroundImage = "url(https://piranhatc.com.au/wp-content/custom-products/1628/bottom-3.jpg)";
function checkImage(url) {
var http = new XMLHttpRequest();
http.open('HEAD', url, false);
http.send();
if (http.status == 200) {
return 'url(' url ')';
} else {
document.getElementById('' parentdiv).remove();
}
}
.bottom-banner {
object-fit: cover;
width: 200px;
height: 200px;
}
<section class="bottom-banner" id="imagebanner">
</section>
id 中的名稱不匹配。下面寫的函式似乎無關緊要。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/348947.html
標籤:javascript html css
上一篇:使用結構隱式宣告函式
