簡體   English   中英

無法讀取未定義的屬性“長度”?

[英]cannot read property 'length' of undefined?

我從在線課程上復制了確切的javascript代碼,但是該代碼對我來說無法正常工作。 嘗試運行它時,我繼續收到“無法讀取未定義的屬性'length'的信息”錯誤。 誰能告訴我為什么我繼續出現此錯誤以及如何解決它。

 //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(); 

將getFigures函數更改為此:

function getFigures() {
    var myObj = document.getElementById('carousel').getElementsByTagName('figure');
    return myObj;
}

當您逐級選擇DOM元素時,javascript無法返回DOM元素。

一件事:在moveForward函數中:為'pointer'提供默認值

var pointer = 0;

因為也許數字標簽沒有可見的類,並且您的代碼已停止

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM