javascript 索引或大小为负或大于允许的数量(非负索引)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5313201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Index or size is negative or greater than allowed amount (non-negative indices)
提问by KongMD
While using Firefox, I keep getting the error described in the subject line for this block of code:
在使用 Firefox 时,我不断收到此代码块主题行中描述的错误:
for(var i = 0; i < tables.length; i++)
{
var j = rows.length - 1;
while(j--)
{
if(hideDP && tables[i].innerHTML.indexOf(">D<") != -1)
{
if(!platTag && !soulSilverTag && pearlTag)
{
tables[i].deleteRow(j);//ERROR IS ON THIS LINE
}
}
}//end while loop (rows)
}//end for loop (tables)
I suspect that this error is because I'm somewhat new to making reverse loops, but I specifically made a reverse loop in this instance because it made deleting rows from a table easier. Note also that j is something like 24 and i is 0, so they're non-negative. Could someone shed some light on this for me?
我怀疑这个错误是因为我对创建反向循环有点陌生,但我在这个例子中专门做了一个反向循环,因为它使从表中删除行变得更容易。还要注意 j 类似于 24 而 i 是 0,所以它们是非负的。有人可以为我解释一下吗?
EDIT :The full code can be found here.
编辑:完整的代码可以在这里找到。
回答by Brock Adams
Strictly working off of the currently posted code, here are the issues I see:
严格处理当前发布的代码,以下是我看到的问题:
The posted code looks incomplete. Where is
rows
being initialized? This could cause the stated error.Given
while(j--)
; ? Thevar j = rows.length - 1;
line is incorrect. That is, unlessyou know that the last row will never need deleting. But if that is the case, then comment the code to make it clear.For example, if there were 4 rows, the current code initializes
j
to 3, but because of the location of the--
operator, the inside of the loop sees: 2, 1, 0. ? For the code as shown, usevar j = rows.length;
or add a comment to show that the logic is deliberate.The 2
if()
statements do not depend onj
at all! (At least as the code is posted here.) If this is true, then move the conditionals outside of thej
loop.Consider posting the full, unedited, code. Or linking to it on a site like Pastebin.
发布的代码看起来不完整。在哪里
rows
初始化?这可能会导致所述错误。给定
while(j--)
; ? 该var j = rows.length - 1;
行不正确。也就是说,除非您知道最后一行永远不需要删除。但如果是这种情况,请注释代码以使其清楚。例如,如果有4行,当前代码初始化
j
为3,但由于--
操作符的位置,循环内部看到:2,1,0。?对于所示的代码,使用var j = rows.length;
或添加注释以表明该逻辑是经过深思熟虑的。这两个
if()
语句根本不依赖j
!(至少当代码在这里发布时。)如果这是真的,那么将条件移到j
循环之外。考虑发布完整的、未经编辑的代码。或者在像Pastebin这样的网站上链接到它。
Update for full script, now that it's been linked to:
更新完整脚本,现在它已链接到:
Scanning the complete code, it looks like tables[i].deleteRow(j);
can be called multiple times for the same row.
扫描完整代码,看起来tables[i].deleteRow(j);
可以为同一行多次调用。
The easy solution, that should be done anyway, is to add a continue
statement after each row delete.
无论如何都应该完成的简单解决方案是continue
在每行删除后添加一条语句。
For extra credit, reanalyze and simplify the flag and if
logic too. :)
对于额外的信用,也重新分析和简化标志和if
逻辑。:)
Update for target page, now that it's been linked to:
更新目标页面,现在它已链接到:
Examining the target page, the tables being looped by this script contain nested tables.
检查目标页面,此脚本循环的表包含嵌套表。
That throws off the row count in this line:var rows = tables[i].getElementsByTagName("tr");
这抛出了这一行中的行数:var rows = tables[i].getElementsByTagName("tr");
Sometimes making it seem like table[i] has more rows than it really directly owns.
有时使它看起来像 table[i] 的行数比它真正直接拥有的行数多。
Solution, use the built in rows array; so the line becomes:
var rows = tables[i].rows;
解决方案,使用内置的rows数组;所以该行变为:
var rows = tables[i].rows;
~~~~
While examining the script relative to the target page, a few other issues seemed apparent:
~~~~
在检查相对于目标页面的脚本时,其他一些问题似乎很明显:
It's not best to loop through all tables. Target just the ones you need. So this:
tables = document.getElementsByTagName("table");
Should be changed to:
var tables = document.querySelectorAll ("div.KonaBody > table.roundy");
...which will select just the 4 payload tables, and not their subtables or the other tables scattered about.
By fine-tuning the initial table selection, the following, problamatic, test is not needed:
if(tables[i].getAttribute("style").indexOf("border: 3px solid") != -1)
- Missing
var
in front of themajorSections
initialization.
最好不要遍历所有表。只针对您需要的对象。所以这:
tables = document.getElementsByTagName("table");
应改为:
var tables = document.querySelectorAll ("div.KonaBody > table.roundy");
...这将只选择 4 个有效载荷表,而不是它们的子表或其他分散的表。
通过微调初始表选择,不需要以下有问题的测试:
if(tables[i].getAttribute("style").indexOf("border: 3px solid") != -1)
var
前面的majorSections
初始化丢失。
回答by JaredMcAteer
That error will also be generated if j is >= to the amount of rows in the table, but I'm not seeing the exact problem.
如果 j >= 表中的行数,也会产生该错误,但我没有看到确切的问题。