Manipulating HTML and Text Content (^) ❘ 119
The preceding example makes it obvious that jQuery’s wrap() method is used to wrap each element
present in a selection individually.
Wrapping a Collection of Elements
Whereas the wrap() method wraps each item present in a selection individually, jQuery’s wrapAll()
method wraps all items present in a selection within a single wrapper. The following document
presents the same markup and style sheet as you saw in the last section for the demonstration of the
wrap() method. The only item that is changed is that the wrapAll() method is used instead of the
wrap() method.
In the following script, Example 4-10, which uses the same markup and style sheet as Example 4-9,
you see that the wrap() method has been swapped for the wrapAll() method:
$(document).ready(
function()
{
$('p').wrapAll('
}
);
Figure 4-15 shows that instead of each
element being individually wrapped in a
you fi nd that all three
elements are wrapped with a single
by the styles used in your style sheet.
FIGURE 4-15
As you can see in the preceding example, the wrapAll() method takes a selection of elements and
collectively wraps the whole selection with a single wrapper element.
Wrapping an Element’s Contents
The last wrapper method demonstrates the wrapInner() method, which wraps an element’s contents.
This method works similarly to the wrap() method in that a wrapper is applied to each item in a