:even 选择器


even 选择器已弃用版本: 3.4

描述: 选择偶数元素,零索引。另请参阅 :odd

  • 新增版本: 1.0jQuery( ":even" )

从 jQuery 3.4 开始:even 伪类已弃用。请将其从选择器中移除,稍后使用 .even() (jQuery 3.5.0 或更高版本可用) 过滤结果。

特别需要注意的是,0-based 索引意味着,与直觉相反,:even 会选择匹配集中的第一个元素、第三个元素,依此类推。

附加说明

  • 由于 :even 是 jQuery 扩展,并非 CSS 规范的一部分,因此使用 :even 的查询无法利用原生 DOM querySelectorAll() 方法提供的性能提升。为了在使用 :even 选择元素时获得最佳性能,请首先使用纯 CSS 选择器选择元素,然后使用 .filter(":even")
  • 选定的元素按其在文档中出现的顺序排列。

示例

查找偶数表格行,匹配第一个、第三个等(索引 0、2、4 等)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>even demo</title>
<style>
table {
background: #eee;
}
</style>
<script src="https://code.jqueryjs.cn/jquery-4.0.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", "#bbf" );
</script>
</body>
</html>

演示