:even 選擇器


even 選擇器已棄用版本:3.4

說明:選取偶數元素,從 0 開始編號。另請參閱 :odd

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

自 jQuery 3.4 起:even 偽類已被棄用。請從選擇器中移除它,並使用 .even() (在 jQuery 3.5.0 或更新版本中可用) 後續篩選結果。

特別注意,從 0 開始編號表示,與直覺相反的是,: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.jquery.com/jquery-3.7.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>

示範