<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>向上查找</title>
<style>
div{
width: 100px;
height: 100px;
background: #ff0;
margin: 10px;
}
.div{
width: 500px;
height: 500px;
background: #f0f;
}
.div1{
width: 300px;
height: 300px;
background: #0ff;
}
</style>
<script type="text/javascript" src="../js/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
$(function(){
//直接父元素(单一)
// $(".div11").parent().css("border","1px solid red");
//所有父级元素div1,div,body,html,直到html
// $(".div11").parents().css("border","1px solid red");
// 不包括div
// $(".div11").parentsUntil(".div").css("border","1px solid blue");
// offsetParent
$(".div11").offsetParent(".div1").css("border","5px solid pink");
// closeSet
// $(".div11").closest(".div1").css("border","5px solid red");
});
</script>
</head>
<body>
<div class="div">
<div class="div1">
<div class="div11">1</div>
<div class="div12">2</div>
</div>
<div class="div2">3</div>
</div>
</body>
</html>
向上查找
最新推荐文章于 2023-01-28 17:04:21 发布