jQuery in Action
Managing the wrapped element set
45
content, including markup and attribute values of children elements, but it
doesn't match markup or attribute values of the original elements being tested.
doesn't match markup or attribute values of the original elements being tested.
The last method that we'll examine in this section is one that allows us to test a
wrapped set to see if it contains at least one element that matches a given selector
expression. The
wrapped set to see if it contains at least one element that matches a given selector
expression. The
is()
method returns
true
if at least one element matches the
selector, and
false
if not. For example:
var hasImage = $('*').is('img');
This statement sets the value of the
hasImage
variable to
true
if the current page
has an image element.
2.3.6 Managing jQuery chains
We've made a big deal (and will continue to do so, because it is a big deal) about
the ability to chain jQuery wrapper methods together to perform a lot of activity
in a single statement. This chaining ability not only allows us to write powerful
operations in a concise manner, but it also improves efficiency because wrapped
sets do not have to be recomputed in order to apply multiple commands to them.
the ability to chain jQuery wrapper methods together to perform a lot of activity
in a single statement. This chaining ability not only allows us to write powerful
operations in a concise manner, but it also improves efficiency because wrapped
sets do not have to be recomputed in order to apply multiple commands to them.
Depending upon the methods used in a command chain, multiple wrapped sets
may be generated. For example, using the
clone()
method (which we'll explore in
Command syntax: contains
contains(text)
Returns a new wrapped set composed of elements that contain the text string passed as the
text
text
parameter
Parameters
text
(String) The text that an element must contain in order to be added to the returned set
Returns
The newly created wrapped set
The newly created wrapped set
Command syntax: is
is(selector)
Determines if any element in the wrapped set matches the passed selector expression
Parameters
selector
(String) The selector expression to test against the elements of the wrapped set
Returns
true
true
if at least one element matches the passed selector; false if not