Пример:
Поиск первого подходящего элемента, соответствующего селектору, указанному в выражении, путём передвижениея вверх по структуре DOM.
"HTML"
<div class="yetparent"> <div class="parent"> <div class="first"> <div class="last"> </div> </div> </div> </div>
"CSS"
.yetparent { width:300px; height:300px; margin: 3px; padding: 3px; background: blue; }
.parent{ width:200px; height:200px; margin: 3px; padding: 3px; background: #EEEEEE; }
.first { width:100px; height:100px;background: yellow; }
.last { width:50px; height:50px;background: green; }
"Живой пример 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(){
$('.first').closest('.yetparent')
.css('background-color', 'red');
});
</script>
</head>
<body class="iframe">
<div class="yetparent">
<div class="parent">
<div class="first">
<div class="last">
</div>
</div>
</div>
</div>
</body>
</html>
<style>
.yetparent { width:300px; height:300px; margin: 3px; padding: 3px; background: blue; }
.parent{ width:200px; height:200px; margin: 3px; padding: 3px; background: #EEEEEE; }
.first { width:100px; height:100px;background: yellow; }
.last { width:50px; height:50px;background: green; }
</style>
Пример:
Поиск первого подходящего элемента соответствующего селектору, указанному в выражении, путём передвижением вверх по структуре DOM.
"jQuery"
var close = $("li:first").closest(["ul", "body"]); $.each(close, function(i){ $("li").eq(i).html( this.selector + ": " + this.elem.nodeName ); });
"HTML"
<div class="yetparent"> <div class="parent"> <div class="first"> <div class="last"> </div> </div> </div> </div>
"CSS"
.yetparent { width:300px; height:300px; margin: 3px; padding: 3px; background: blue; }
.parent{ width:200px; height:200px; margin: 3px; padding: 3px; background: #EEEEEE; }
.first { width:100px; height:100px;background: yellow; }
.last { width:50px; height:50px;background: green; }
"Живой пример 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(){
var close = $("li:first").closest(["ul", "body"]);
$.each(close, function(i){
$("li").eq(i).html( this.selector + ": " + this.elem.nodeName );
});
});
</script>
</head>
<body class="iframe">
<div class="yetparent">
<div class="parent">
<div class="first">
<div class="last">
</div>
</div>
</div>
</div>
</body>
</html>
<style>
.yetparent { width:300px; height:300px; margin: 3px; padding: 3px; background: blue; }
.parent{ width:200px; height:200px; margin: 3px; padding: 3px; background: #EEEEEE; }
.first { width:100px; height:100px;background: yellow; }
.last { width:50px; height:50px;background: green; }
</style>