jQuery prevAll()
get all
<!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").prevAll().css("background-color", "yellow"); });//from w ww . jav a2s .co m </script> </head> <body> <p>This is a p element.</p> <div>This is a div element.</div> <p>This is a p element.</p> <h4>This is a h4 element.</h4> <h5>This is a h5 element.</h5> <span>This is a span element.</span> <p>This is a p element.</p> </body> </html>