您的位置:首页 > Web前端 > CSS

从$('li').filter(':even').css('background-color', 'red');说起

2016-01-13 10:43 531 查看
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)。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: