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

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


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

Definition and Usage
定义与用法

The slice() method returns selected elements from an existing array.
slice()方法可将现存数组中的选中元素返回

Syntax
语法

arrayObject.slice(start,end)

Parameter
参数
Description
描述
start Required. Specify where to start the selection. Must be a number
必要选项。指定开始的位置。必须是数字
end Optional. Specify where to end the selection. Must be a number
可选项。指定结束的位置。必须是数字


Tips and Notes
提示与注意点

Tip: You can use negative numbers to select from the end of the array.
提示:你可以使用负数从数组的后面来势选择

Note: If end is not specified, slice() selects all elements from the specified start position and to the end of the array.
注意:如果结束位置没有指定,那么slice()会从选择开始位置到数组的最后位置。


Example
举例

In this example we will create an array, and then display selected elements from it:
在这个举例中我们将建立一个数组。并且选择性的显示其中的一部分元素:

<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.slice(1) + "<br />")
document.write(arr)

</script>

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

Jani,Hege,Stale
Hege,Stale
Jani,Hege,Stale


Example
举例

In this example we will create an array, and then display selected elements from it:
在这个举例中我们将建立一个数组,并将选中的元素显示出来:

<script type="text/javascript">

var arr = new Array(6)
arr[0] = "Jani"
arr[1] = "Hege"
arr[2] = "Stale"
arr[3] = "Kai Jim"
arr[4] = "Borge"
arr[5] = "Tove"

document.write(arr + "<br />")
document.write(arr.slice(2,4) + "<br />")
document.write(arr)

</script>

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

Jani,Hege,Stale,Kai Jim,Borge,Tove
Stale,Kai Jim
Jani,Hege,Stale,Kai Jim,Borge,Tove


Try-It-Yourself Demos
尝试与演示

slice()
How to use slice() to display selected elements from an existing array.
怎样使用slice()来从现有的数组中选择性的显示其中的元素。

评论 (0) All

登陆 | 还没注册?