jQuery: Getting the two last list items?

痴心易碎 提交于 2019-11-29 09:09:14

Another approach, using andSelf and prev:

$('ul li:last-child').prev('li').andSelf().addClass("special");
vkostromin

You can use function slice. It is very flexible.

$('ul li').slice(-2).addClass("special");
var items = $('ul li')
var last_two = items.filter('li:gt('+ items.length-3 +')')
last_two.addClass('special');
$(function(){

  $("li:gt("+($("li").length-3)+")").addClass("special");

});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!