jQuery slice()Method

jQuery 遍历方法jQuery Traversal Methods

Examples

Select <p> elements starting from the <p> element with index number 2:

$("p").slice(2)

Try it Yourself »

Definition and Usage

The slice() method selects a subset of elements based on index.

A subset is a set that is part of a larger set.

This method is used to limit the selection of elements in a collection by specifying a start point and an end point:startThe parameter is the start index for creating the subset (starting from 0),stopThe parameter is an optional end point.


Syntax

$(selector).slice(start,stop)

Parameter Description
start Required. Specifies the position from which to start selecting elements. The index number starts from 0.

Note:If it is a negative number, it indicates that elements are selected from the end of the selected elements, rather than from the beginning.
stop Optional. Specifies the position to stop selecting elements. If omitted, the selection range ends at the end of the collection. The index number starts from 0.

Note:If it is a negative number, it indicates that elements are selected from the end of the selected elements, rather than from the beginning.

实例

More Examples

start and stop
How to use these two parameters to select <p> elements from the start point to the end point.

Using negative numbers
Use negative numbers to select <p> elements starting from the end, rather than from the beginning.

Using negative numbers for both parameters
Use negative start and stop parameters to select <p> elements from the end.


jQuery 遍历方法jQuery Traversal Methods

Other extensions