【发布时间】:2013-01-02 13:38:06
【问题描述】:
我有这个函数试图改变 img 的 src 属性。这是Javascript:
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>
什么给了?
【问题讨论】:
-
你什么时候调用这个函数?
-
setTimeout()调用必须是setTimeout(transition, 1000); -
@SLaks 我认为很明显它是在元素出现在页面上之前被调用的,否则递归就会被破坏。
-
你能在 JsFiddle 中重现这个吗?
-
图片显示后是否调用该函数?还是在正文完全加载之前进行了 javascript 调用?
标签: javascript null document getelementbyid