02.06.2013 Views

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

46<br />

CHAPTER 2 ■ COMMON JQUERY ACTIONS AND METHODS<br />

.wrap()<br />

This duplicates the result from before (see Figure 2-14).<br />

The .wrap() method allows developers to enclose existing elements with one or more new elements<br />

quickly <strong>and</strong> easily.<br />

The argument accepted <strong>by</strong> .wrap() can either be a collection of one or more tags to wrap around the<br />

selected elements, or a callback function to generate the tags.<br />

First, wrap all the spans in the example document with a strong tag using the following:<br />

$("span").wrap("");<br />

This results in the text of the two spans becoming bold (see Figure 2-8).<br />

Figure 2-8. The spans appear bold after wrapping them with strong tags<br />

The syntax used for the wrapping element is relatively forgiving, <strong>and</strong> the output shown in Figure 2-7<br />

could have been accomplished using either "", "", or "".<br />

Additionally, multiple tags can be wrapped around elements <strong>by</strong> passing a nested set of tags to the<br />

.wrap() method:<br />

$("span").wrap("");<br />

After executing the preceding line, the text in the spans will appear bold <strong>and</strong> italicized (see<br />

Figure 2-9).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!