filter() 方法將匹配元素集合縮減爲匹配指定選擇器的元素。
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
</head>
<body>
<ul>
<li>list item 1</li>
<li>list item 2</li>
<li>list item 3</li>
<li>list item 4</li>
<li>list item 5</li>
<li>list item 6</li>
</ul>
<script>
$('li').filter(':even').css('background-color', 'red');
</script>
</body>
</html>
:even 選擇器選取每個帶有偶數 index 值的元素(比如 2、4、6)。
index 值從 0 開始,所有第一個元素是偶數 (0)。
:odd 選擇器選取每個帶有奇數 index 值的元素(比如 1、3、5)。
index 值從 0 開始,所有第一個元素是偶數 (0)。