9

代码:

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() 永远找不到它们。这是怎么回事?

4

1 回答 1

29

试试这个:

...
var index = js_array.indexOf(parseInt(name, 10)); // so that it does not try to compare strings...
...
于 2013-01-29T20:58:12.390 回答