:even

选择所引值为偶数的元素,从 0 开始计数。也可以查看 odd.

jQuery(":even")

特别地注意的是,这是 基于0的索引 ,所以 :even 选择器是选择第一个元素,第三个元素,依此类推在匹配。

注意

  • 因为 :even 是一个 jQuery 延伸出来的选择器,并不是的CSS规范的一部分,使用 :even 查询不能充分利用原生DOM提供的 querySelectorAll() 方法来提高性能。为了当使用 :even 的时候在现代浏览器上获得更佳的性能,首先使用纯CSS选择器选择元素,然后使用 .filter(":even") .
  • 查找表格中索引值是偶数的行(即实际表格中的奇数行),即匹配第一行、第三行、第五行等(索引值是 0, 2 ,4 等)。

例子

Finds even table rows, matching the first, third and so on(index 0, 2, 4 etc.).

<!DOCTYPE html>
<html>
<head>
  <style>
  table {
    background:#eeeeee;
  }
  </style>
  <script src="./static/js/jquery-3.5.0.js"></script>
</head>
<body>
  <table border="1">
    <tr><td>Row with Index #0</td></tr>
    <tr><td>Row with Index #1</td></tr>
 
    <tr><td>Row with Index #2</td></tr>
    <tr><td>Row with Index #3</td></tr>
  </table>
<script>$("tr:even").css("background-color", "#bbbbff");</script>
 
</body>
</html>

上篇: :eq(index)

下篇: :odd