开发者

jQuery Advance Selector

开发者 https://www.devze.com 2023-01-08 13:46 出处:网络
<ul id=\"comment1\"> <li>reply1</li> <li>reply2</li> <li>reply3</li>
<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

精彩评论

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