如何在jQuery中使用jQuery.closest()方法?
该closest()方法从当前元素开始,并且仅返回与传递的表达式匹配的第一个祖先。返回的jQuery对象具有零个或一个元素。
示例
您可以尝试运行以下代码,以了解如何使用jQuery最接近的方法:
<!DOCTYPE html>
<html>
<head>
<style>
.myclass * {
display: block;
border: 2px solid blue;
color: red;
padding: 5px;
margin:10px;
}
</style>
<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("span").closest("ol").css({"color": "gray", "border": "3px solid yellow"});
});
</script>
</head>
<body class="myclass">body
<div style="width:500px;">div
<ol>ol - This is the third ancestor
<ol>ol - This is the second ancestor
<ol>ol - This is the first ancestor
<li>li - This is the direct parent element
<span>demo</span>
</li>
</ol>
</ol>
</ol>
</div>
</body>热门推荐
1 如何训练狗狗握手
10 新员工寄语祝福语简短
11 新婚敬酒祝福语大全简短
12 怎么训练狗狗
13 训练狗狗上厕所
14 春蚕老师祝福语简短励志
15 高考好运如愿祝福语简短
16 战哥过年祝福语简短
17 中考感谢朋友祝福语简短
18 长沙旅游祝福语大全简短