这是我的代码,我不确定我是否使用正确的循环,但基本上这是要做的。我试图让数组中的每个src打印出每一秒,但当试图打印出myPix数组中的第二项时,myPix [thisPic]变得未定义。我真的很乐意得到一些帮助。谢谢!了解Java脚本中的循环
function nextPic() {
count = -1;
thisPic = 0;
if (thisPic == count) {
document.write("This shouldn't ever happen");
return false;
} else {
count++;
var myPix = new Array("images/slideshow/Jellyfish.jpg", "images/slideshow/Penguins.jpg", "images/slideshow/Lighthouse.jpg");
slideTag = document.getElementsByTagName("img")[1];
slideTag.src = myPix[thisPic];
currPic = slideTag.src;
document.write(myPix[thisPic] + "<br />");
document.write(currPic);
}
return false;
}
setInterval(nextPic, 1000);
它确实有助于格式化您的代码,因此可以轻松读取它。 – Dropout
你似乎并没有在任何地方更新'thisPic',并且你没有在任何地方使用'count',只是将它与'thisPic'比较,它总是'false'。因此,'myPix [thisPic]'不应该导致'undefined'。 'myPix'是一个至少包含一个元素的数组。你有没有从你的代码中省略一些东西? –
在页面加载完成后使用'document.write' [某些时候会严重结束](http://stackoverflow.com/questions/5985740/not-allowed-document-write-after-page-load) 。 –