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

JavaScript
JS数学对象参考
JS字符串对象参考
JS函数参考
JS事件参考
Javascript 常用正则表达式
FF和IE下的js兼容性问题
jQuery 简单介绍
jQuery / 核心 / $(expression, [context] ) 函数
jQuery / 核心 / $(html) 函数
如何使用JS来判断浏览器类型(ie、firefox,等等)
Javascript在IE和FireFox中的不同表现
3个js字符编码函数区别
javascript 中的 XMLDOM 对象

JavaScript unshift()方法


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-04   浏览: 367 ::
收藏到网摘: 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

登陆 | 还没注册?