2012-08-30 42 views
-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> 

输出 -为什么Jquery:odd选择偶数元素?

1 
pawned 
3 
pawned 

回答

5

,因为它利用基于0的索引。

1是0指数(这是偶数)等等。

Reference

阵列是基于0的在JS,和jQuery对象涡卷元件以阵列状结构。当然,大多数jQuery方法利用基于0的索引,除非另有明确说明 - 如nth-child(),由于严格来源于CSS spec,所以它是1-索引的。

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."

Reference

相关问题