我认为.splice()的意思可能是错的,但我认为它删除了一个数组元素。我想在这里做的就是删除“梨子”,但它不起作用:
var my_array = ["apples","pears","bananas","oranges"];
my_array.splice($.inArray("pears",my_array));
$.each(my_array, function(k,v) {
document.write(v+"<br>");
});
答案 0 :(得分:8)
你错过了两个论点:
代码变为:
var my_array = ["apples","pears","bananas","oranges"];
my_array.splice($.inArray("pears", my_array), 1);
$.each(my_array, function(k,v) {
document.write(v+"<br>");
});
答案 1 :(得分:4)
var my_array = ["apples","pears","bananas","oranges"];
my_array.splice($.inArray("pears", my_array), 1);
$.each(my_array, function(k,v) {
document.write(v+"<br>");
});
答案 2 :(得分:2)
这对我有用:http://jsfiddle.net/HbjHV/
var my_array = ["apples","pears","bananas","oranges"];
var pos = $.inArray("pears", my_array);
pos !== -1 && my_array.splice(pos, 1);
$.each(my_array, function(k,v) {
document.write(v+"<br>");
});
答案 3 :(得分:1)
您需要将数组传递给$ .inArray,并将要删除的元素数传递给array.splice:
var my_array = ["apples","pears","bananas","oranges"];
my_array.splice($.inArray("pears", my_array), 1);
$.each(my_array, function(k,v) {
document.write(v+"<br>");
});
答案 4 :(得分:1)
试试这个
my_array.splice($.inArray("pears", my_array), 1);
答案 5 :(得分:0)
答案 6 :(得分:0)
请查看what arguments .splice()
方法确实收到了!