<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>
how to select last 3 or 4 answers using jquery?
You are looking for a slicemethod:
slice
To get the 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
Source: https://habr.com/ru/post/1756403/More articles:How to align a switch in android - androidСмутно с наследованием С++ - c++give MySQL or PHP work? - performancePeriodically check if Android Android has new data - performanceImmutable Object in Objective-C: Big init Method? - immutabilityиспользование разреза для циклических данных - rWhat is the best way to prevent the browser from displaying images until they are fully loaded? - javascriptautomatically save php variables without a database - variablesCreating XML schema from JAXB class files in Ant - javaChecking an empty input number - pythonAll Articles