代码:
function showlayer(name){
var size = js_array.length
var index = js_array.indexOf(name);
var plusOne = js_array[index+1];
document.write("" + name + "<br />" + js_array + "<br />" + index + "<br />" +
plusOne + "<br />" )
...
}
输出:
301
300,299,301,290,303,304,302,310,291,306,308,305,307,292,294,295,309
-1
300
name的所有可能值都在数组中,但由于某种原因,indexOf()永远不会找到它们。怎么了?
答案 0 :(得分:26)
请改为尝试:
...
var index = js_array.indexOf(parseInt(name, 10)); // so that it does not try to compare strings...
...