why does $('tr:even').addClass('alt');
select from the 1st row
and $('tr:nth-child(even)').addClass('alt');
selects from the 2nd row ?
views:
72answers:
3
+6
A:
It's because :even gets even elements using 0-based indexing and :nth-child() uses 1-based indexing.
BBonifield
2010-01-22 17:01:22
http://api.jquery.com/even-selector/
artlung
2010-01-22 17:06:23
then why $('tr:even').addClass('alt') selects index 1, should not it select index 2 (Even)?
amipax
2010-01-22 17:16:22
A:
maybe the selector looks at the index and nth-child(even) looks at the length? Seems like an oversight.
hunter
2010-01-22 17:03:09
A:
got it! :odd and :even selectors use Javascript native zero based numbering. so the first row counts as 0(even)
amipax
2010-01-22 18:24:03