只要使用 splice function 就 ok 囉~
範例 :
<script language="javascript">
/* Array.splice(要插入新元素的位置, */
/* 要刪除插入新元素位置後的舊元素個數, */
/* 新元素值1, */
/* 新元素值2, */
/* ... ) */
function Case1()
{
myArray = new Array(3);
myArray[0] = "Row1";
myArray[1] = "Row2";
myArray[2] = "Row3";
myArray.splice(myArray.length ,0 ,"Row4");
/* result: Row1 Row2 Row3 Row4 */
}
function Case2()
{
myArray = new Array(3);
myArray[0] = "Row1";
myArray[1] = "Row2";
myArray[2] = "Row3";
myArray.splice(1 ,1 ,"Row4", "Row5");
/* result: Row1 Row4 Row5 Row3 */
}
</script>
/* Array.splice(要插入新元素的位置, */
/* 要刪除插入新元素位置後的舊元素個數, */
/* 新元素值1, */
/* 新元素值2, */
/* ... ) */
function Case1()
{
myArray = new Array(3);
myArray[0] = "Row1";
myArray[1] = "Row2";
myArray[2] = "Row3";
myArray.splice(myArray.length ,0 ,"Row4");
/* result: Row1 Row2 Row3 Row4 */
}
function Case2()
{
myArray = new Array(3);
myArray[0] = "Row1";
myArray[1] = "Row2";
myArray[2] = "Row3";
myArray.splice(1 ,1 ,"Row4", "Row5");
/* result: Row1 Row4 Row5 Row3 */
}
</script>
沒有留言:
張貼留言