当我开始显示这就是我得到的:Uncaught TypeError: Cannot read property 'src' of undefined
指向function
:(runPicGal
见下文)。希望你能帮忙。注意:奇怪的是我看到图片改变了一次然后我得到了错误。
完整的 JS 文件:
var picForDiashow;
var diashowPicArray = [];
var diashowPicArrayIndex = 0;
diashowPicArray [0] = new Image(); diashowPicArray [0].src = "./pics/1.jpg";
diashowPicArray [1] = new Image(); diashowPicArray [0].src = "./pics/2.jpg";
diashowPicArray [2] = new Image(); diashowPicArray [0].src = "./pics/3.jpg";
diashowPicArray [3] = new Image(); diashowPicArray [0].src = "./pics/4.jpg";
diashowPicArray [4] = new Image(); diashowPicArray [0].src = "./pics/5.jpg";
function initForDiashow () {
picForDiashow = document.getElementById("diashowPic");
}
window.onload = initForDiashow;
function startDiashow () {
picInterval = window.setInterval(runPicGal, 1000);
}
function stopDiashow () {
window.clearInterval(picInterval);
}
function runPicGal () {
diashowPicArrayIndex++;
if (diashowPicArrayIndex > diashowPicArray.length) {
diashowPicArrayIndex = 0;
}
picForDiashow.src = diashowPicArray[diashowPicArrayIndex].src;
}