jQuery slice()
by negative start and negative end
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("p").slice(-3, -1).css("background-color", "yellow"); });//from ww w . ja v a2s . c o m </script> </head> <body> <h1>Welcome to My Homepage</h1> <p>This is a test (index 0).</p> <p>CSS (index 1).</p> <p>I am from java2s.com (index 2).</p> <p>This is another test (index 3).</p> <p>And I'm in love with Daisy (index 4).</p> </body> </html>