嗨,我正在为IE8 compat模式调试我的页面,这个脚本就是不喜欢工作和崩溃。
基本上,它必须遍历一个3D数组,并向一个变量添加一个本地路径。我可以不这样做,但我只是好奇为什么它从来不起作用.
欢迎任何建议:)下面是代码:
for(i=0;i<menu_items_p.length;i++)
for(j=0;j<menu_items_p[i].length;j++)
menu_items_p[i][j][1]='http://127.0.0.1/'+menu_items_p[i][j][1];
数组看起来是这样的:
var menu_items_p =
[
[ //Products
['Health Care', 'products/health.php'],
['Aroma Therapy','products/scents.php'],
],
[ // Empty
],
[ //Test
['What ever', 'spirulina/about.php'],
]
]
但问题是,它有时有空值,而array.length会触发一些错误.
发布于 2012-05-13 18:55:06
正如Yoshi和ThiefMaster所建议的,我这样做是为了解决这个问题:
for(var i=0;i<menu_items_p.length;i++)
if (menu_items_p[i] !== undefined)
for(var j=0;j<menu_items_p[i].length;j++)
if (menu_items_p[i][j] !== undefined)
menu_items_p[i][j][1]='http://127.0.0.1/'+menu_items_p[i][j][1];
用于未定义. replaced.
很遗憾他们没有以正式的方式回答,所以我不得不回答我自己:)谢谢大家!
发布于 2012-05-07 11:20:25
使用原始数组声明时:
var menu_items_p =
[
[ //Products
['Health Care', 'products/health.php'],
['Aroma Therapy','products/scents.php'],
],
[ // Empty
],
[ //Test
['What ever', 'spirulina/about.php'],
]
]
错误发生在IE8中,而不在IE9中。只需删除两个逗号:
var menu_items_p =
[
[ //Products
['Health Care', 'products/health.php'],
['Aroma Therapy','products/scents.php'] // here comma removed
],
[ // Empty
],
[ //Test
['What ever', 'spirulina/about.php'] // here comma removed
]
]
一切都必须正常工作。
发布于 2012-05-07 11:20:43
也许您的代码可以这样处理空值:
for(var i = 0; i < menu_items_p.length; i++) {
// we skip the value if it is empty or an empty array
if(!menu_items_p[i] || !menu_items_p[i].length) continue;
for(var j = 0; j < menu_items_p[i].length; j++) {
// again, we skip the value if it is empty or an empty array
if(!menu_items_p[i][j] || !menu_items_p[i][j].length) continue;
menu_items_p[i][j][1] = 'http://127.0.0.1/' + menu_items_p[i][j][1];
}
}
https://stackoverflow.com/questions/10480867
复制相似问题