js用remove删除数组,提示remove是“undefined”,解决方案

js用remove删除数组,提示remove是“undefined”时,需要扩写remove 方法:


*删除数组指定下标或指定对象
*/
Array.prototype.remove = function(obj) {
for ( var i = 0; i < this.length; i++) {
var temp = this[i];
if (!isNaN(obj)) {
temp = i;
}
if (temp == obj) {
for ( var j = i; j < this.length; j++) {
this[j] = this[j + 1];
}
this.length = this.length - 1;
}
}
}


用法:
function delUser(id,eName){
var spanEname = $("#spanEname").val();

var eArray = spanEname.split(",");
var newEArray = new Array();
for ( var i = 0; i < eArray.length; i++) {
if(eName == eArray[i]){
eArray.remove(i);
}
}
spanEname = eArray.join(",");
$("#spanEname").val(spanEname);
}

你可能感兴趣的:(JavaScript,XHTML)