splice()可向数组删除并加入新的元素。
语法
arrayObject.splice(index,howmany,element1,.....,elementX)
<script type="text/javascript"> var arr = new Array(5) arr[0] = "Jani" arr[1] = "Hege" arr[2] = "Stale" arr[3] = "Kai Jim" arr[4] = "Borge" document.write(arr + "<br />") arr.splice(2,0,"Lene") document.write(arr + "<br />") </script>
输出结果为:
Jani,Hege,Stale,Kai Jim,Borge
Jani,Hege,Lene,Stale,Kai Jim,Borge
arr.splice(2,1,"Tove")
输出结果为:
Jani,Hege,Stale,Kai Jim,Borge
Jani,Hege,Tove,Kai Jim,Borge
arr.splice(2,3,"Tove")
输出结果为:
Jani,Hege,Stale,Kai Jim,Borge
Jani,Hege,Tove
来源:https://www.cnblogs.com/xcj1989/archive/2011/03/26/1996387.html