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
精彩评论