我从我上网的课程中复制了完整的javascript代码,但代码对我来说无法正常工作。我继续得到"无法读取属性'长度'未定义"尝试运行它时出错。任何人都可以告诉我为什么我继续得到这个错误以及如何解决它。
//the JavaScript logic on this page simply adds the "visible" CSS class to the next image in the rotation appoximately every 3.5 seconds
var slideInterval = 3500;
//retrieves all of the "figure" elements within the "section" element using the "id" of 'carousel'. Returns the resulting array as the result of this function
function getFigures() {
return
document.getElementById('carousel').getElementsByTagName('figure');
}
//This function iterates over the figure elements in the section element. It removes the visible class from the current figure element, then adds the class to the next figure element. Once complete, it uses the setTimeout function to invoke itself again after a specified amount of time (3500 milliseconds = 3.5 seconds)
function moveForward() {
var pointer;
var figures = getFigures();
for (var i = 0; i < figures.length; i++) {
if (figures[i].className == 'visible') {
figures[i].className = '';
pointer = i;
}
}
if (++pointer == figures.length) {
pointer = 0;
}
figures[pointer].className = 'visible';
setTimeout(moveForward, slideInterval);
}
//In the startPlayback function, use the setTimeout function in JavaScript to invoke the moveForward method after a specified amount of time. Use the slideInterval variable for the time interval
function startPlayback() {
setTimeout(moveForward, slideInterval);
}
//invokes "startPlayback" from above
startPlayback();
&#13;
答案 0 :(得分:0)
将getFigures函数更改为:
function getFigures() {
var myObj = document.getElementById('carousel').getElementsByTagName('figure');
return myObj;
}
当您逐级选择时,javascript无法返回DOM元素。
一件事: 在moveForward函数中:为'pointer'提供默认值
var pointer = 0;
因为也许数字标签没有可见的类 并且您的代码已停止