Selectors
Links
Methods
find
find
is useful when you already have a jQuery object (passed to function,
declared earlier etc):
var item = jQuery('li.item-ii');
// do something
item.find('li').css('background-color', 'red');
function(table) {
jQuery(table).find('tbody')
Selectors
a href
If you’d like to select an element by attribute rather than by HTML elements, you can use XPath expressions to select elements with a specific attribute:
Attribute Selector |
Description |
---|---|
|
select all elements with href attribute |
|
select all elements with href value equal to “#” |
|
select all elements with href attribute NOT equal to “#” |
|
select all elements with an href attribute that ends with “.png” |
For attributes:
|
is exactly equal |
|
is not equal |
|
starts with |
|
ends with |
|
contains |
For example:
$('a[href$="ABC"]:first').attr('title');
…will return the title of the first link that has a URL which ends with “ABC”.
Elements
You can use special selectors to choose specific HTML elements:
Element Selector |
Description |
---|---|
|
select all |
|
select all |
|
select all |
|
selects the current HTML element |
Exists
To test whether an element exists, use the length
property of the jQuery
collection returned by your selector:
if ( $('#myDiv').length )
$('#myDiv').show();
Note: that it isn’t always necessary to test whether an element exists. The following code will show the element if it exists, and do nothing (with no errors) if it does not:
$('#myDiv').show();