jQuery siblings() 舉例說明
jQuery 中的 siblings() 方法用於返回所選元素的所有兄弟元素。
語法
語法如下 −
$(selector).siblings(filter)
以上,該濾鏡指定一個選擇器表示式,以縮小對兄弟元素的搜尋範圍。
現在我們來看一個示例,以實現 jQuery siblings() 方法 −
示例
<!DOCTYPE html>
<html>
<head>
<style>
div {
width:600px;
}
.demo * {
display: block;
background-color;
border: 2px solid yellow;
color: blue;
padding: 10px;
margin: 10px;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("li.test").siblings().css({"background-color": "orange","color": "white", "border": "3px dashed blue"});
});
</script>
</head>
<body>
<h2>Demo Heading</h2>
<div class="demo">
<ul>ul (parent)
<li>previous sibling</li>
<li>previous sibling</li>
<li>previous sibling</li>
<li class="test">li (sibling)</li>
<li>next sibling</li>
<li>next sibling</li>
<li>next sibling</li>
</ul>
</div>
</body>
</html>輸出
這將產生以下輸出−

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