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.

Figure 2-5. The exp<strong>and</strong>ed element, including the dynamically added text<br />

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

You can use this technique throughout the rest of the exercises in this book to see where content <strong>and</strong><br />

elements are being added to the DOM.<br />

Using .append() <strong>and</strong> .prepend(), you can also add new elements to the DOM. For instance, to add a<br />

new paragraph at the top of the browser page, prepend a new element to the body using this code:<br />

var para = $("", {<br />

"text":"I'm a new paragraph!",<br />

"css":{"background":"yellow"}<br />

});<br />

$("body").prepend(para);<br />

■ Note This example uses a variable to store the new element before prepending it to the body. This is done to<br />

increase the legibility of the script. You’ll be using this technique often throughout this book.<br />

43

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

Saved successfully!

Ooh no, something went wrong!