开发者

Apply class to every 4th li inside a ul

I need to display 4 li elements in a row, then apply a class="last" to every 4th li element.

right now i am doing like this,

var liCount = jQuery('.expl开发者_JS百科oreContentArea .listArea ul li').size();
jQuery('.exploreContentArea .listArea ul li:eq(3)').addClass('last');

i need to apply the class to every 4th item of the ul.

Please help me on the same.

Thanks | Lokesh Yadav


Use nth-child:

jQuery('.exploreContentArea .listArea ul li:nth-child(4n)')
.addClass('last').show();


Change size() to length as it's a bit faster I think because no function call. Sarfraz's answer is quite right.


you could just use the css-selector :)

.exploreContentArea .listArea ul li:nth-child(4n)

edit: here's some more info on this :)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜