JavaScript数组的splice()方法改变数组的内容,增加了新的元素,同时消除旧元素。
语法
array.splice(index, howMany, [element1][, ..., elementN]);
下面是参数的详细信息:
返回值:
返回基于传递的参数提取数组。
例子:
<html><head><title>JavaScript Array splice Method</title></head><body><script type="text/javascript">var arr = ["orange", "mango", "banana", "sugar", "tea"];var removed = arr.splice(2, 0, "water");document.write("After adding 1: " + arr );document.write("<br />removed is: " + removed);removed = arr.splice(3, 1);document.write("<br />After adding 1: " + arr );document.write("<br />removed is: " + removed);</script></body></html>
这将产生以下结果:
After adding 1: orange,mango,water,banana,sugar,tearemoved is: After adding 1: orange,mango,water,sugar,tearemoved is: banana
新闻热点
疑难解答