Я скопировал точный код javascript из класса, который я принимаю онлайн, но код не работает должным образом для меня. При попытке запустить его я продолжаю получать ошибку «не могу прочитать свойство» неопределенной ошибки. Может ли кто-нибудь сказать мне, почему я продолжаю эту ошибку и как я могу это исправить.не может прочитать свойство 'length' undefined?
//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();
Удалить разрыв строки между 'return' и' document.getElementById ... '. JS по существу рассматривает их как два отдельных заявления, когда вы вставляете разрыв строки. Таким образом, это должно быть просто 'return document.getElementById ('carousel'). GetElementsByTagName ('figure');'. –
Можете ли вы также поделиться html-кодом. –
[ASI] (http://stackoverflow.com/questions/2846283/what-are-the-rules-for-javascripts-automatic-semicolon-insertion-asi) не ваш друг. Закрытие как опечатка. – ssube