With jQuery you have the ability to select elements of a web page by their position. For example if you want to select the first
<a> on the page you will write:
If you want to retrieve links starting from the third one on the page you will write the following code:
gt means “Greater Than”. The argument
1 is the index from which to start. If you have any kind of programming background you will know that indexes usually start at 0. So, greater than 1 means the third element!
Here you have the position filters supported by jQuery:
:first– Selects the first match (Example:
li a:firstreturns the
first anchor that’s a descendant of a list item).
:last– Selects the last match (Example:
li a:lastreturns the
last anchor that’s a descendant of a list item).
:even– Selects even elements (Example:
even-indexed list item).
:odd– Selects odd elements (Example:
li:oddreturns every oddindexed list item).
:eq(n)– Selects the nth matching element (Example:
p:eq(2)selects the paragraph at index 2).
:gt(n)– Selects elements after the nth matching element (Example:
p:gt(2)selects the paragraph at index 3).
:lt(n)Selects elements before the nth matching element (Example:
p:lt(2)selects the paragraph at index 1).
:odd are related to the index of the elements within the set.
You can pass a negative value to
:lt() so the counting starts from the last element.