我有一个方法可以返回给定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上发现了许多SIMILAR问题,但没有一个与我的确切情况相符。我尝试改变这种方法,包括使用spanArray.push(span)
和spanArray.push.apply(spanArray, span)
添加我的跨度,但无济于事。我没有想法。
答案 0 :(得分:1)
在错误消息中,我可以发现r
太多:
Uncaught ReferenceError: spansArrray is not defined
^^^
似乎是另一个错字,不是在你发布的代码中,而是在你执行的代码中......
答案 1 :(得分:0)
更改为:
var spansArray = getAllSpansForID(verseID);
var length = spansArray.length;
答案 2 :(得分:0)
的 D'OH !! 强> 的
正如您在错误消息中看到的那样,spansArrray
(3个r)未定义。我定义了spansArray
(2 r)。我修好了,一切顺利。只是一个错字....
我没有直接从代码中解除length = spansArray.length;
,而是将其写出来,因此这里发布的代码不会失败。
对不起大家。我非常感谢所有的帮助!!!