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

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 unshift()方法


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

Definition and Usage
定义与用法

The unshift() method adds one or more elements to the beginning of an array and returns the new length.
unshift()方法可向数组的开始部位加入一个或多个元素并返回新的长度

Syntax
语法

arrayObject.unshift(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.
注意点:这个方法将改变数数组的长度

Note: The unshift() method does not work properly in Internet Explorer!
注意点:IE里unshift()方法的运行效果不理想!

Tip: To add one or more elements to the end of an array, use the push() method.
提示:如要为数组的末尾部位加上一个或多个元素,可以使用push()方法


Example
举例

In this example we will create an array, add an element to the beginning of the array and then return the new length:
在这个举例我们将建立数组,并在它的开始部位加入一个元素并返回数组的新长度:

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

The output of the code above will be:
输出结果:

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


Try-It-Yourself Demos
尝试与演示

unshift()
How to use unshift() to add an element to the beginning of an array and return the new length.
怎样使用unshift()方法来为数组的开头加入元素,并返回数组的新长度

评论 (0) All

登陆 | 还没注册?