02.06.2013 Views

DOM Traversal Methods

DOM Traversal Methods

DOM Traversal Methods

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.

Universal: *<br />

All elements.<br />

Examples<br />

1. $('*'): selects all elements in the document<br />

2. $('p > *'): selects all elements that are children of a paragraph element<br />

Description<br />

The * selector is especially useful when combined with other elements to form a<br />

more specific selector expression.<br />

[ 25 ]<br />

Chapter 2<br />

XPath Selectors<br />

Modeled after a file system's directory-tree navigation, XPath selector expressions<br />

provide an alternative way to access <strong>DOM</strong> elements. Although XPath was developed<br />

as a selector language for XML documents, jQuery makes a basic subset of its<br />

selectors available for use in XML and HTML documents alike.<br />

For more information about XPath 1.0, visit the specification at the W3C:<br />

http://www.w3.org/TR/xpath.<br />

Descendant: E//F<br />

All elements matched by F that are descendants of an element matched by E.<br />

Examples<br />

1. $('div//code'): selects all elements matched by that are<br />

descendants of an element matched by <br />

2. $('//p//a'): selects all elements matched by that are descendants of an<br />

element matched by <br />

Description<br />

This XPath descendant selector works the same as the corresponding CSS descendant<br />

selector ($('E F')) except that the XPath version can specify that it is to start at the<br />

document root, which could be useful when querying an XML document.<br />

In example 2, the initial //p tells jQuery to start at the document root and match<br />

all elements that are descendants of it. Keep in mind that if this selector<br />

expression follows a <strong>DOM</strong> traversal method such as .find(), this syntax will not<br />

select anything because the document root cannot be a child of anything else. Since<br />

jQuery allows free mixing of CSS and XPath selectors, the initial // is redundant and,<br />

therefore, can be omitted.

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

Saved successfully!

Ooh no, something went wrong!