开发者

add a class to every other three lines in jquery?

开发者 https://www.devze.com 2023-03-23 22:50 出处:网络
<ul id=\"开发者_Python百科test\"> <li>test</li> <li>test</li> <li>test</li> // add a class to this li
<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

0

精彩评论

暂无评论...
验证码 换一张
取 消