Javascript 创建跨度数组的函数始终返回未定义的
我有一个方法,它返回给定id模式的跨度数组。当我在函数末尾打印出数组的值进行测试时,确实创建了这个数组,并且所有正确的元素似乎都在其中 这就是功能:Javascript 创建跨度数组的函数始终返回未定义的,javascript,android-webview,Javascript,Android Webview,我有一个方法,它返回给定id模式的跨度数组。当我在函数末尾打印出数组的值进行测试时,确实创建了这个数组,并且所有正确的元素似乎都在其中 这就是功能: function getAllSpansForID(sectionID) { var foundAllSpans = false, i = 1, spanID, span, spanArray = new Array(); /* Keep looking until
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;
}
始终会产生以下错误:
Uncaught ReferenceError: spansArrray is not defined
由于作用域问题,我在returnign数组上发现了许多类似的问题,但没有一个与我的实际情况相匹配。我已尝试更改此方法,包括使用spanArray.push(span)
和spanArray.push.apply(spanArray,span)
添加我的跨度,但没有效果。我没有主意了。换成:
var spansArray = getAllSpansForID(verseID);
var length = spansArray.length;
D'OH强>
从我的错误消息中可以看出,spansarray
(3 r)未定义。我定义了spansArray
(2个r)。我修好了,一切正常。只是一个打字错误
我没有提起length=spansArray.length代码>直接从代码中获取,而只是将其写出,因此此处发布的代码不会失败
对不起大家。我同样感谢所有的帮助 在错误消息中,我可以发现r
过多:
Uncaught ReferenceError: spansArrray is not defined
^^^
似乎是另一个输入错误,不是在您发布的代码中,而是在您执行的代码中…为什么一个函数名为一次GetAllSpansForrid,另一个函数名为getAllSelectionSpansForVerse?这真的是一样的吗?spanArray!==spansArray
@Andreas他们在函数内部称之为spansArray
,在函数外部称之为spansArray
,这样应该没问题。你有没有像Chrome开发工具那样通过调试器运行它?还有,你看到任何控制台消息了吗?我复制并粘贴到了JSFIDLE中,好吧,这也可以修复OP的打字错误:-),但与他在问题中发布的内容没有功能上的区别。
Uncaught ReferenceError: spansArrray is not defined
^^^