幻灯片可以在 Firefox 中运行。它曾经在 IE 和 Chrome 中运行。现在我在 IE 和 Chrome 中都收到以下错误:
Uncaught TypeError: Cannot set property 'src' of undefined
该脚本使用 <script type="...>
链接在文档头中。
网页中的代码如下:
<section style="margin: 0 auto; text-align: center;">
<img src="./rPix/rp1.jpg" id="slide" width="900" height="200" alt="slide show images" />
</section>
<body onload="runShow();">
函数runShow
是slideshow.js的一部分 - 代码如下:
/* An automatically rotating slide show using Javascript and the DOM.
This script cobbled together by Paul D.J. Vandenberg */
var j = 1;
var pix = new Array(11);
for (i = 0; i < pix.length; i++) {
pix[i] = "rPix/rp"+j+".jpg";
j++;
}
var index = Math.floor(Math.random() * 11) + 1;
var limit = pix.length - 1;
function runShow() {
if (index > limit) {
index = 0;
}
document.slide.src = pix[index];
setTimeout("runShow()", 10000);
index++;
}
最佳答案
确保在 id="slide"
元素添加到 DOM 后调用 runShow()
。
document.slide
是 document.getElementById("slide")
的简写。当没有定义具有该 id 的元素时,后者将返回 null
。
关于javascript - 未捕获的类型错误 : Cannot set property 'src' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16993907/