2016-06-27 13 views
0

オンラインで撮影しているクラスの正確な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();

+5

停止した理解ので間に改行を削除します'return'と' document.getElementById ... 'です。 JSは、改行を入れるときに、本質的にそれらを2つの別個のステートメントとして扱います。だから、 'return document.getElementById( 'carousel')。getElementsByTagName( 'figure');'だけです。 –

+0

htmlコードも共有できますか? –

+1

[ASI](http://stackoverflow.com/questions/2846283/what-are-the-rules-for-javascripts-automatic-semicolon-insertion-asi)はあなたの友人ではありません。タイプミスとして閉じます。 – ssube

答えて

0

変更getFigures機能:

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

javascriptのは、あなたがそれらをレベルによってレベルを選択するDOM要素を返すことができません。

一つの問題:moveForward機能で :「ポインタ」

var pointer = 0; 

のデフォルト値を与えるかもしれないタグが見えクラス ていなかったし、あなたのコードが

関連する問題