<ul id="开发者_Python百科test">
<li>test</li>
<li>test</li>
<li>test</li> // add a class to this li
<li>test</li>
<li>test</li>
<li>test</li> // add a class to this li
<li>test</li>
<li>test</li>
<li>test</li> // add a class to this li
</ul>
how to use jquery to add a class to the above li lines which i add a comment. thank you.
$('#test > li:nth-child(3n)').addClass('foo');
Demo: http://jsfiddle.net/mattball/32tKY/
API docs: http://api.jquery.com/nth-child-selector/
Here is an excellent breakdown on how nth-child works at CSS Tricks.
you can do it like this
$(document).ready(function(){
$('ul li:nth-child(3n)').addClass('blah');
});
check out this jsfiddle
精彩评论