Пример:
Определить все элементы div перед последним div и установить для них css свойтсво.
"HTML"
<div></div> <div></div> <div></div> <div></div>
"CSS"
div { width:70px; height:70px; background:#abc;
border:2px solid black; margin:10px; float:left; }
div.before { border-color: red; }
"Живой пример jQuery"
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link href="http://slyweb.ru/css/jqueryiframe.css"
rel="stylesheet" type="text/css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("div:last").prevAll().addClass("before");
});
</script>
</head>
<body class="iframe">
<div></div>
<div></div>
<div></div>
<div></div>
</body>
</html>
<style>
div { width:70px; height:70px; background:#abc;
border:2px solid black; margin:10px; float:left; }
div.before { border-color: red; }
</style>
Пример:
Найти немедленно следующий предшествующий элемент потомок каждого параграфа с классом "selected".
"HTML"
<div><span>Hello</span></div> <p class="selected">Hello Again</p>And Again
"Живой пример jQuery"
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link href="http://slyweb.ru/css/jqueryiframe.css"
rel="stylesheet" type="text/css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("p").prev(".selected").css("background", "yellow");
});
</script>
</head>
<body class="iframe">
<div><span>Hello</span></div>
<p class="selected">Hello Again</p>
And Again
</body>
</html>