我有這個函數試圖改變img的src屬性。下面是使用Javascript:獲取「無法設置null的屬性'src'」,但元素存在
function transition(){
document.getElementById("firstfirst").src = marray[currArrayValue];
currArrayValue++;
if(currArrayValue == array.length-1){
currArrayValue = 0;
}
setTimeout(transition(), 1000);
}
我的谷歌瀏覽器控制檯是說的document.getElementById(「firstfirst」)不存在,但它絕對不會。這裏的HTML:
<div id="banners-container">
<div id="banners">
<img src="images/banners/top-banner-one.png" id="firstfirst" alt="Subscribe now to get access to thousands of vintage movies!" border="0">
</div>
</div>
什麼給?
你什麼時候調用函數? – SLaks
'setTimeout()'調用需要'setTimeout(transition,1000);' – Pointy
@SLaks我認爲很清楚它在元素在頁面之前被調用,否則遞歸會被炸燬。 – Pointy