02.06.2013 Views

DOM Traversal Methods

DOM Traversal Methods

DOM Traversal Methods

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

<strong>DOM</strong> <strong>Traversal</strong> <strong>Methods</strong><br />

Cloning jQuery Objects<br />

When a jQuery object is passed as a parameter to the $(), a new jQuery object<br />

is created that references the same <strong>DOM</strong> elements. The initial object can then be<br />

modified without affecting the new one.<br />

Creating New Elements<br />

If a string is passed as the parameter to $(), jQuery examines the string to see<br />

if it looks like HTML. If not, the string is interpreted as a selector expression, as<br />

explained above. But if the string appears to be an HTML snippet, jQuery attempts<br />

to create new <strong>DOM</strong> elements as described by the HTML. Then a jQuery object is<br />

created and returned that refers to these elements. We can perform any of the usual<br />

jQuery methods on this object:<br />

$('My new paragraph').appendTo('body');<br />

The actual creation of the elements is handled by the browser's innerHTML<br />

mechanism. Specifically, jQuery creates a new element and sets the<br />

innerHTML property of the element to the HTML snippet that was passed in. This<br />

means that to ensure cross-platform compatibility, the snippet must be well-formed.<br />

Tags that can contain other elements should always be paired with a closing tag:<br />

$('');<br />

Tags that cannot contain elements should be quick-closed:<br />

$('');<br />

Filtering <strong>Methods</strong><br />

These methods remove elements from the set matched by a jQuery object.<br />

.filter()<br />

Reduces the set of matched elements to those that match the selector or pass the<br />

function's test.<br />

.filter(selector)<br />

.filter(function)<br />

Parameters (first version)<br />

•<br />

selector: A string containing a selector expression to match elements against<br />

[ 40 ]

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

Saved successfully!

Ooh no, something went wrong!