$() Factory Function, CSS Selectors, XPath Selectors, Custom Selectors

| No Comments | No TrackBacks

No matter which type of selector we want to use in jQuery - be it CSS, XPath, or custom - we always start with the dollar sign and parentheses: $()

$() Factory Function
Let's see some more common examples:
A tag name: $('p') gets all paragraphs in the document.
An ID: $('#some-id') gets the single element in the document that has the corresponding some-id ID.
A class: $('.some-class') gets all elements in the document that have a class of some-class.

CSS Selectors
Chile combinator
>

$('#selected-id > li')

Find each liste item (li) that is a child (>) of an element with an ID of selected-id (#selected-id).

Negation pseudo-class
not

$('#selected-id > li:not(.horizontal)')

Find each list item that do not have a class of horizontal, which is a child of #selected-id.

XPath Selectors

$('a[@title]')

This is a attribute selector, so use @ symbol inside square brackets. Example means that to select all links that have a title attribute.

Here are more examples about XPath Selectors.
Attribute selectors accept regular-expression-like syntax for identifying the beginning(^) or ending($) of a string. And also take an asterisk(*) to indicate an arbitrary position within a string.

$('a[@href^="mailto:"]')

href attribute begins with mailto
$('a[@href$=".pdf"]')

href attribute ends with .pdf.
$('a[@href*="mysite.com"]')

href attribute has mysite.com inside.

Custom Selectors
I just put some sample here.

$('tr:odd')
$('tr:even')
$('td:contains("David")')
$('th').parent()
$('tr:not([th]):even')
$('tr:not[ht]:odd')

The most recommended complicated way.

$('td:contains("David")').parent().find('td:eq(1)').addClass('highlight').end().find('td:eq(2)').addClass('highlight');


Related Articles

No TrackBacks

TrackBack URL: http://www.yinfor.com/mtcgi/mt-tb-0822.cgi/2077

Leave a comment

About this Entry

This page contains a single entry by David Yin published on February 15, 2008 10:29 AM.

Solve the Trouble with a cURL request in PHP was the previous entry in this blog.

MySQL Backup and Recovery Methodologies is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

OpenID accepted here Learn more about OpenID
Powered by Movable Type 4.21-en

Online Tools