JavaScript函数

来源:互联网 发布:c语言log函数怎么调用 编辑:程序博客网 时间:2024/05/17 02:39

slice()方法可将现存数组中的选中元素返回
arrayObject.slice(start,end)

start:   Required. Specify where to start the selection. Must be a number
            必要选项。指定开始的位置。必须是数字

end:   Optional. Specify where to end the selection. Must be a number 
           可选项。指定结束的位置。必须是数字

Tip: You can use negative index numbers to select from the end of the string.
提示:你可以用一个负的起始值从字符串的末尾开始选取字符

Note: If end is not specified, slice() selects all characters from the specified start position and to the end of the string.
注意:如果没有指定结束的位置,slice()将选取从指定起始位置开始到字符串结束的所有字符

实例1

In this example we will extract all characters from a string, starting at position 6:
在本例中,我们将选取字符串中第6个字符以后的所有字符:

<script type="text/javascript">

var str="Hello happy world!"
document.write(str.slice(6))
</script>

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

happy world!


实例2

In this example we will extract all the characters from position 6 to position 11:
在本例中,我们将从字符串中选取第6个字符到第11个字符间的字符:

<script type="text/javascript">

var str="Hello happy world!"
document.write(str.slice(6,11))
</script>

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

happy


实例3

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

实例4

在这个举例中我们将建立一个数组,并将选中的元素显示出来:

<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
原创粉丝点击