Metoda jQuery slice()
Przykład
Rozpocznij selekcję elementów <p> od elementu <p> o numerze indeksu 2:
$("p").slice(2)
Definicja i użycie
Metoda slice() wybiera podzbiór elementów na podstawie jego indeksu.
Podzbiór to zbiór będący częścią większego zbioru.
Ta metoda służy do ograniczenia wyboru elementów w grupie przez punkt początkowy i końcowy: parametr start jest indeksem początkowym (zaczyna się od 0), z którego tworzy się podzbiór, a parametr stop jest opcjonalnym punktem końcowym.
Składnia
$(selector).slice(start,stop)
Parameter | Description |
---|---|
start | Required. Specifies where to start the selection of elements. The index numbers start at 0. Note: Using a negative number will select elements from the end of the selected elements, instead of the beginning. |
stop | Optional. Specifies where to end the selection of elements. If omitted, the selection continues until the end of the set. The index numbers start at 0. Note: Using a negative number will select elements from the end of the selected elements, instead of the beginning. |
Spróbuj sam - przykłady
Jak używać obu parametrów do wybierania elementów <p> z punktu początkowego i końcowego.
Użycie liczby ujemnej do rozpoczęcia selekcji <p> elementów licząc od końca zamiast od początku.
Użycie start i stop z liczbą ujemną, aby rozpocząć selekcję elementów <p> licząc od końca.