from:http://www.jquerycn.cn/a_10447
The splice () method in JavaScript is a strong array method that has many uses.
The main purpose of splice () is to insert items into the middle of the array.
There are 3 ways of doing this:
Delete--You can delete any number of items, just specify 2 parameters: the location of the first item to delete and the number of items to delete.
For example, splice (0,2) deletes the first two items in the array.
Insert--You can insert any number of items to the specified location, providing only 3 parameters: Knight position, 0 (number of items to delete), and items to insert.
If you want to insert more than one item, you can pass in four, five, any number of items.
For example, splice (2,1, "Red", "green") removes the entry for the current array position 2, and then inserts the string "Red" and "green" starting at position 2.
Replace--You can insert any number of items at the specified location and delete any number of items at the same time by specifying 3 specified parameters: The starting position, the number of items to delete, and any number of items to insert.
The image inserted does not have to be equal to the number of items deleted. For example, splice (2,2, "Red", "green") removes the entry for the current array position 2, and then inserts the string "Red" and "green" starting at position 2.
The splice () method always returns an array that contains the items removed from the array of elements (an empty array is returned if no items are deleted).
example, the way to use the Splice method:
Copy Codecode example:
<script>
var colors = ["Red", "green", "blue"];
var removed = Colors.splice (0,1); Delete First item
alert (colors); Green,blue
alert (removed); Red, the value in the returned array contains an item
removed = Colors.splice (1, 0, "yellow", "orange"); Insert two items starting from position 1
alert (colors); Green,yellow,organge,blue
alert (removed); An empty array is returned.
removed = Colors.splice (1, 1, "Red", "purple"); Insert two items to delete an item
alert (colors); Green,red,purple,orange,blue
alert (remove); Yellow, the returned array contains only one item
</script>
Guess:
Parameter one: operation (additions and deletions) Start position
Parameter two: delete operation length
Parameter n (>2): Increment parameter
Delete First, then add
Splice () method for detailed JavaScript