jQuery 元素 ~ 兄弟選擇器


jQuery 中的元素 ~ 兄弟選擇器用於選擇指定“元素”的所有兄弟元素。

語法

語法如下:

("ele ~ siblings")

上面,引數 ele 是任何有效的選擇器,而 siblings 是 ele 引數的兄弟元素。

示例

現在讓我們來看一個實現 jQuery **元素** **~ 兄弟選擇器** 的例子:

線上演示

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
   $(document).ready(function(){
      $("div ~ p").css("color", "orange");
   });
</script>
<style>
div {
   border:1px solid black;
   padding:10px;
}
</style>
</head>
<body>
<h2>Demo Heading</h2>
<div>
<p>This is demo text.</p>
<span>span outside p element</span>
<p>This is demo text.</p>
</div>
<p>p next to div.</p>
<p>This is demo text.</p>
<div>
<p>This is demo text.</p>
<p>This is demo text. <span>span inside p element.</span></p>
<p>This is demo text.</p>
</div>
</body>
</html>

輸出

這將產生以下輸出:

更新於:2019年12月31日

瀏覽量:117

啟動你的 職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.