帶示例的 jQuery closest()
jQuery 中的 closest() 方法用於返回已選擇元素的第一個祖先元素。
語法
語法如下 −
$(selector).closest(filter)
例項
現在,讓我們舉個例子來實現 jQuery closest() 方法 −
<!DOCTYPE html>
<html>
<head>
<style>
.demo * {
display: block;
border: 2px solid blue;
padding: 5px;
margin: 15px;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("span").closest("ol").css({"background-color": "orange", "color": "black","border": "3px dashed orange"});
});
</script>
</head>
<body class="demo">body
<div style="width:600px;">
<ol>ol
<ol>ol
<ol>ol
<li>li
<span>span</span>
</li>
</ul>
</ul>
</ul>
</div>
</body>
</html>輸出
這會產生以下輸出 −

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP