jQuery in Action

42
CHAPTER 2
Creating the wrapped element set
of the wrapped elements. We can also subset the wrapped set, based on the posi-
tion of the elements within the set. Let's see which methods allow us to do that.
Obtaining subsets of the wrapped set
Sometimes we may wish to obtain a subset of the wrapped set, based on the posi-
tion of elements within the set. jQuery provides a method to do that named
slice()
. This command creates and returns a new set from any contiguous por-
tion, or a slice, of an original wrapped set. The syntax for this command follows:
If we want to obtain a wrapped set that contains a single element from another
set, based on its position in the original set, we could employ the
slice()
method,
passing the zero-based position of the element within the wrapped set. For exam-
ple, to obtain the third element, we write
$('*').slice(2,3);
This statement selects all elements on the page and then generates a new set con-
taining the third element in the matched set.
Note that this is different from
$('*').get(2)
, which returns the third element
in the wrapped set, not a wrapped set containing the element.
Therefore, a statement such as
$('*').slice(0,4);
selects all elements on the page and then creates a set containing the first four
elements.
To grab elements from the end of the wrapped set, the statement
$('*').slice(4);
Command syntax: slice
slice(begin,end)
Creates and returns a new wrapped set containing a contiguous portion of the matched set.
Parameters
begin
(Number) The zero-based position of the first element to be included in the
returned slice.
end
(Number) The optional zero-based index of the first element not to be included in
the returned slice, or one position beyond the last element to be included. If omit-
ted, the slice extends to the end of the set.
Returns
The newly created wrapped set.


Другие страницы

 
Cкачать книги бесплатно без регистрации в электронном виде (pdf, chm, txt).Вы можете читать книги онлайн на нашем сайте литературного портала книг.Большая подборка учебников, пособий, интересных книг.Электронные книги на английском языке скачать бесплатно без смс.