jQuery Even element selector

Description and Syntax

$(':even')

select all elements with an even index within the matched set.

:even selects the first element, third element, and so on within the matched set.

Examples

  • $('li:even') selects the even-indexed elements within the set of <li> elements
  • $('.myclass:even') selects the even-indexed elements within the set of elements that have the class myclass

<html>
  <head>
    <script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){<!--   www  .ja va2 s  . c o  m-->
                
            $("tr:even").css("background-color", "red");

                
        });
    </script>

  </head>
  <body>
    <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>

    </body>
</html>

Click to view the demo





















Home »
  jQuery »
    jQuery Tutorial »




Basics
Selector
DOM
Event
Effect
Utilities