.slice( start [, end ] )Returns: jQuery
Description: Reduce the set of matched elements to a subset specified by a range of indices.
-
version added: 1.1.4.slice( start [, end ] )
-
startType: IntegerAn integer indicating the 0-based position at which the elements begin to be selected. If negative, it indicates an offset from the end of the set.
-
endType: IntegerAn integer indicating the 0-based position at which the elements stop being selected. If negative, it indicates an offset from the end of the set. If omitted, the range continues until the end of the set.
-
.slice()
method constructs a new jQuery object containing a subset of the elements specified by the start
and, optionally, end
argument. The supplied start
index identifies the position of one of the elements in the set; if end
is omitted, all elements after this one will be included in the result.
1
2
3
4
5
6
7
|
|
1
|
|
1
|
|
Negative Indices
.slice()
method is patterned after the JavaScript .slice() method for arrays. One of the features that it mimics is the ability for negative numbers to be passed as either the start
or end
parameter. If a negative number is provided, this indicates a position starting from the end of the set, rather than the beginning. For example:
1
|
|
-2
) and one from the end (-1
).
Examples:
Example 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
|
Demo:
Example 2
1
|
|
Example 3
1
|
|
Example 4
1
|
|
Example 5
1
|
|
Example 6
1
|
|