-2
<html>
 <head> 
   <script type="text/javascript" src="jquery-1.7.1.js" ></script>
   <script type="text/javascript">
    $(function() {
       $("p:odd").html('pawned');
    });
   </script>
 </head>
<body>
  <p>1</p>
  <p>2</p>
  <p>3</p>
  <p>4</p>
</body>
</html>

输出 -

1
pawned
3
pawned
4

3 回答 3

5

因为它使用基于 0 的索引。

1是 0 索引(它是偶数)等等。

参考

数组在 JS 中是从 0 开始的,而 jQuery 对象将元素包装在类似数组的结构中。自然,大多数 jQuery 方法都使用基于 0 的索引,除非另有明确说明 - as nth-child(),由于严格派生自CSS 规范,它是 1-indexed 。

于 2012-08-30T18:44:33.273 回答
4

"In particular, note that the 0-based indexing means that, counter-intuitively, :odd selects the second element, fourth element, and so on within the matched set."

参考

于 2012-08-30T18:45:44.877 回答
2

索引从 0 开始。因此结果请阅读:http ://api.jquery.com/odd-selector/

于 2012-08-30T18:44:59.650 回答