jQuery :odd and :nth-child CSS3 different

两盒软妹~` 提交于 2019-12-24 03:43:04

问题


I found that jQuery :odd selector and CSS3 nth-child(odd) works different. http://jsfiddle.net/TMDwT/5/

In yellow it's CSS nth-child(odd) and if you uncomment JS and comment background: yellow in CSS you will find that it found in another way.

Can anybody say how I achieve the same result as in jQuery but with CSS3?

Thanks!


回答1:


Yes, :odd and :nth-child(odd) are not the same thing:

  • :odd matches the odd items within the matched elements, i.e. the contents of the jQuery object you apply the selector to,

  • :nth-child(odd) matches the odd items within their respective parents.

This is the same difference as between :first and :first-child, or :last and :last-child.

Update: As zzzzBov and BoltClock rightfully point out, the :odd selector is zero-based but the :nth-child() selector is one-based. This means that even if you apply the two selectors to the complete child list of an element (thus removing the difference between :odd and :nth-child(odd)), they still won't match the same elements.



来源:https://stackoverflow.com/questions/10044910/jquery-odd-and-nth-child-css3-different

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!