当前位置: 首页 > 网络学院 > 客户端脚本教程 > JavaScript > JavaScript push()方法

JavaScript
JS 字符串
JS Date
JS数组,JS Array
JS Boolean
JS Math
JS HTML DOM
JS Browser
JS Cookies
JS 校验
JS Animation
JS Image Maps
JS Timing
JS 建立对象
JS 摘要
JS 实例
JS 对象实例
JS DOM 实例
JS数组对象参考
JS布尔对象参考
JS日期对象参考

JavaScript push()方法


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-04   浏览: 450 ::
收藏到网摘: n/a

Definition and Usage
定义与用法

The push() method adds one or more elements to the end of an array and returns the new length.
使用push()方法可以向数组的末尾添加一个或多个元素并且返回新的长度

Syntax
语法

arrayObject.push(newelement1,newelement2,....,newelementX)

Parameter
参数
Description
描述
newelement1 Required. The first element to add to the array
必要选项。向数组添加的第一个元素
newelement2 Optional. The second element to add to the array
可选项。向数组添加的第二个元素
newelementX Optional. Several elements may be added
可选项。可添加多个元素


Tips and Notes
提示与注意点

Note: This method changes the length of the array.
注意:这个方法会改变数组的长度

Tip: To add one or more elements to the beginning of an array, use the unshift() method.
提示:要向数组的头部加入一个或多个元素,可以使用unshift()方法


Example
举例

In this example we will create an array, and then change the length of it by adding a element:
在这个举例中我们将建立一个数组,并且接着改变加入元素后数组的长度:

<script type="text/javascript">
var arr = new Array(3)
arr[0] = "Jani"
arr[1] = "Hege"
arr[2] = "Stale"
document.write(arr + "<br />")
document.write(arr.push("Kai Jim") + "<br />")
document.write(arr)
</script>

The output of the code above will be:
上面代码的输出结果为:

Jani,Hege,Stale
4
Jani,Hege,Stale,Kai Jim


Try-It-Yourself Demos
尝试与演示

push()
How to use push() to change the length of an array.
怎样使用push()来改变数组的长度

评论 (0) All

登陆 | 还没注册?