特定の ID パターンのスパンの配列を返すメソッドがあります。この配列は作成され、関数の最後に配列の値を出力してテストすると、適切な要素がすべて含まれているように見えます。
これは機能です:
function getAllSpansForID(sectionID) {
var foundAllSpans = false,
i = 1,
spanID,
span,
spanArray = new Array();
/* Keep looking until we found all the selection spans.*/
while (!foundAllSpans) {
spanID = sectionID + "-" + i;
span = document.getElementById(spanID);
/*
If we didn't get a span we can assume there are no more to find.
We are done with this loop.
*/
if (span == null) {
foundAllSpans = true;
console.log("Found all spans.");
}
/*
Else, add the span to the array we are going to return.
*/
else {
spanArray[i-1] = span;
i++;
}
}
console.log("returning spanArray.length: " + spanArray.length);
for (i = 0; i < spanArray.length; i++) {
console.log("spanArray[i].id: " + spanArray[i].id);
console.log("spanArray[i].outerHTML: " + spanArray[i].outerHTML);
}
return spanArray;
}
私の問題は、この関数を呼び出すたびに、戻り値が常に未定義であることです。
このコード:
var spansArray = getAllSpansForID(verseID),
length = spansArray.length;
常に次のエラーが発生します。
Uncaught ReferenceError: spansArrray is not defined
SO では、スコープの問題が原因で returnign 配列を使用して多くの類似の問題を発見しましたが、私の正確な状況と一致するものはありませんでした。を使用してスパンを追加するなど、この方法を変更しようspanArray.push(span)
としましたが、役に立ちませんでした。spanArray.push.apply(spanArray, span)
私はアイデアがありません。