3

http://jsfiddle.net/dPwQA/2/

说我将一个新项目推入一个数组,排序后,我想获取它的索引。

function sortInt(a, b) {
    return a - b;
}
numbers = [7,6];
numbers.sort(sortInt);
$('#text').text(numbers.toString());

$('button').click(function () {
    numbers.push('4');
    alert(numbers.indexOf("6")); // doesn't work
    numbers.sort(sortInt);
    $('#text').text(numbers.toString());
});
4

2 回答 2

0

用这个

alert(numbers.indexOf(6));

在推 4 的同时,你也应该这样做

numbers.push(4)

代替

numbers.push('4')

因为 '4' 将 4 作为字符串而不是数字。

希望这可以帮助...

于 2013-10-18T06:46:34.353 回答
0

删除 . 周围的引号6

它应该是:

alert(numbers.indexOf(6));

代替:

alert(numbers.indexOf("6"));

编辑:

当我的意思是删除周围的引号时6,我应该说到处都删除。

这应该变成:

numbers.push(4);

相对于:

numbers.push('4');

于 2013-10-18T06:38:17.627 回答