开发者

jQuery Advance Selector

<ul id="comment1">
 <li>reply1</li>
 <li>reply2</li>
 <li>reply3</li>
 <li>reply4</li>
 <li>reply5</li>
 <li>reply6</li>
 <li>reply7</li>
 <li>reply8</li>
</ul>
开发者_StackOverflow

how to select last 3 or 4 replies with jquery?


You are looking for slice method:

To get last four items (updated with -4 as suggested by @KennyTM):

$('ul#comment1 li').slice(-4).css('color', '#00ff00');


$('#comment1 li:last-child')
     .prev('li').andSelf()
     .prev('li').andSelf()
     .prev('li').andSelf()
     .prev('li').andSelf()

Should get the last four

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜