为什么我的变量myImage在调试器中出现为Null

时间:2017-05-24 10:58:25

标签: javascript

我是学习javascript和编程的新手。我从学习网站上复制了这个片段。加班我加载页面我在调试器中得到myImage为空的错误?有人可以帮忙解释一下吗?

谢谢!

var myImage = document.getElementById("mainImage");

var imageArray = ["_images/heretic.jpg","_images/prudential.jpg","_images/woofs_logo.jpg"
                ];
var imageIndex = 0;

function changeImage() {
    myImage.setAttribute( "src",imageArray[imageIndex]);
    imageIndex++;
    if (imageIndex >= imageArray.length) {
        imageIndex = 0;
    }
}

// setInterval is also in milliseconds
setInterval(changeImage,5000);

1 个答案:

答案 0 :(得分:-1)

也许你没有id =" mainImage" ?