如何在 jQuery 中查詢所有已單擊元素的兄弟元素?
要查詢所有已單擊元素的兄弟元素,使用父元素及其兄弟元素方法,並選擇類來查詢所有兄弟元素。
示例
你可以嘗試執行以下程式碼以查詢所有已單擊元素的兄弟元素
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".target").click(function(){
$(this).parent().siblings().removeClass("selectedClass");
$(this).parent().addClass("selectedClass");
});
});
</script>
<style>
a {
cursor:pointer;
}
.selectedClass{
color:blue;
}
</style>
</head>
<body>
<h1>Countries</h1>
<ul>
<li><a class="target">India</a></li>
<li><a class="target">US</a></li>
<li><a class="target">UK</a></li>
</ul>
</body>
</html>
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP