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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

■ Note The preceding example uses single quotation marks to enclose the string of HTML rather than double<br />

ones. This has no effect on the <strong>jQuery</strong> function; it merely eliminates the need to escape the double quotes used in<br />

the class attribute (e.g., class=\"bat\").<br />

As of <strong>jQuery</strong> 1.4, you can also add attributes to this new element <strong>by</strong> passing a second argument as<br />

JavaScript Object Notation (JSON) 2 :<br />

$("", {<br />

"class":"bat",<br />

"text":"This is a new paragraph!"<br />

});<br />

The result of the above code snippet is the following:<br />

>>> $("", { "class":"bat", "text":"This is a new paragraph!" });<br />

[ p.bat ]<br />

Because this is only creating the element, it hasn’t been attached to the DOM yet <strong>and</strong>, therefore,<br />

isn’t visible in the browser window. You’ll learn to insert new elements in the next section, “Inserting<br />

New Elements into the DOM.”<br />

■ Note At its simplest, JSON is a key-value pairing where both the key <strong>and</strong> value are surrounded <strong>by</strong> quotation<br />

marks <strong>and</strong> all key-value pairs are comma-separated <strong>and</strong> enclosed in curly braces ({}). A sample of JSON data<br />

would be { "key":"value" } or { "key1":"value1", "key2":"value2" }.<br />

Inserting New Elements into the DOM<br />

Now that you have a basic underst<strong>and</strong>ing of how to create new elements, you can begin learning how to<br />

insert them into the DOM. <strong>jQuery</strong> provides several methods for h<strong>and</strong>ling this, which you’ll explore in<br />

this section.<br />

An important note to make here is that the modification of the DOM is temporary, meaning that any<br />

changes made will be reset back to the original HTML document once the page is refreshed. This<br />

happens because JavaScript is a client-side language, which means it isn’t modifying the actual files from<br />

the server, but the browser’s individual interpretation of the file.<br />

2 http://en.wikipedia.org/wiki/Json<br />

39

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

Saved successfully!

Ooh no, something went wrong!