odd()

版本: jQuery3

将匹配元素的集合减少为集合中的奇数,从零开始编号。

.odd()

此方法不接受任何参数。此方式从jQuery3.5添加

给定一个表示一组DOM元素的jQuery对象,该.odd()方法将从该组中的偶数元素构造一个新的jQuery对象。从零开始计数!

考虑一个上面有简单列表的页面:


<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>
</ul>


<script> $( "li" ).odd().css( "background-color", "red" );</script>

该调用的结果是第二和第四项的红色背景。。

例子

突出显示列表中的奇数项。

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>odd demo</title>
  <style>
  .highlight {
    background-color: yellow;
  }
  </style>
  <script src="./static/js/jquery-3.5.0.js"></script>
</head>
<body>
 
<ul>
  <li>Look:</li>
  <li>This is some text in a list.</li>
  <li>This is a note about it.</li>
  <li>This is another note about it.</li>
</ul>
 
<script>
$( "ul li" ).odd().addClass( "highlight" );
</script>
 
</body>
</html>

odd demo
  • Look:
  • This is some text in a list.
  • This is a note about it.
  • This is another note about it.

上篇: even()