Description: Select the element at index n within the matched set.
The index-related selectors (
:odd) filter the set of elements that have matched the expressions that precede them. They narrow the set down based on the order of the elements within this matched set. For example, if elements are first selected with a class selector (
.myclass) and four elements are returned, these elements are given indices
3 for the purposes of these selectors.
$('.myclass:eq(1)') selects the second element in the document with the class myclass, rather than the first. In contrast,
:nth-child(n) uses 1-based indexing to conform to the CSS specification.
<!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-latest.js"></script> </head> <body> <table border="1"> <tr><td>TD #0</td><td>TD #1</td><td>TD #2</td></tr> <tr><td>TD #3</td><td>TD #4</td><td>TD #5</td></tr> <tr><td>TD #6</td><td>TD #7</td><td>TD #8</td></tr> </table> <script>$("td:eq(2)").css("color", "red");</script> </body> </html>