JQuery remove last number of list items
I have a var containing a number and need to subtract this number from the total number of list items. Then I need to remove the end items in the list eg. Total = 20; Number of list items = 18; Final number = 2; Then remove the last 2 list items.
Thanks for any help.开发者_如何学JAVA
I'd use the :gt() selector.
$( 'li:gt(' + ( maxAllowed-1 ) + ')' ).remove();
Demo: http://jsfiddle.net/JAAulde/5GdQx/
$.slice(start, [end])
jQuery Documentation
UPDATE:
Example:
$('li').slice( -numToRemove ).remove();
精彩评论