
- jQuery 教程
- jQuery - 首頁
- jQuery - 路線圖
- jQuery - 概述
- jQuery - 基礎
- jQuery - 語法
- jQuery - 選擇器
- jQuery - 事件
- jQuery - 屬性
- jQuery - AJAX
- jQuery DOM 操作
- jQuery - DOM
- jQuery - 新增元素
- jQuery - 刪除元素
- jQuery - 替換元素
- jQuery CSS 操作
- jQuery - CSS 類
- jQuery - 尺寸
- jQuery - CSS 屬性
- jQuery 效果
- jQuery - 效果
- jQuery - 動畫
- jQuery - 鏈式操作
- jQuery - 回撥函式
- jQuery 遍歷
- jQuery - 遍歷
- jQuery - 遍歷祖先元素
- jQuery - 遍歷子孫元素
- jQuery UI
- jQuery - 互動
- jQuery - 小部件
- jQuery - 主題
- jQuery 參考
- jQuery - 選擇器
- jQuery - 事件
- jQuery - 效果
- jQuery - HTML/CSS
- jQuery - 遍歷
- jQuery - 其他
- jQuery - 屬性
- jQuery - 工具函式
- jQuery 外掛
- jQuery - 外掛
- jQuery - PagePiling.js
- jQuery - Flickerplate.js
- jQuery - Multiscroll.js
- jQuery - Slidebar.js
- jQuery - Rowgrid.js
- jQuery - Alertify.js
- jQuery - Progressbar.js
- jQuery - Slideshow.js
- jQuery - Drawsvg.js
- jQuery - Tagsort.js
- jQuery - LogosDistort.js
- jQuery - Filer.js
- jQuery - Whatsnearby.js
- jQuery - Checkout.js
- jQuery - Blockrain.js
- jQuery - Producttour.js
- jQuery - Megadropdown.js
- jQuery - Weather.js
- jQuery 有用資源
- jQuery - 常見問題解答
- jQuery - 快速指南
- jQuery - 有用資源
- jQuery - 討論
jQuery 元素 ~ 兄弟選擇器
jQuery 中的元素 ~ 兄弟選擇器用於選擇指定元素之後的所有兄弟元素。"~" 選擇器與特定的元素選擇器一起使用,以指定其後的兄弟元素。
語法
以下是 jQuery 元素 ~ 兄弟選擇器的語法:
("element ~ siblings")
引數
以下是上述語法的描述:
- element: 指定選擇其後兄弟元素的元素。
- siblings: `element` 的兄弟元素。
示例 1
在下面的示例中,我們使用 jQuery 的“元素 ~ 兄弟選擇器”來選擇 <h2> 元素之後所有 <p> 元素的兄弟元素:
<html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script> $(document).ready(function(){ // Select all siblings of 'p' elements that come after the first 'p' $("h2 ~ p").css("background-color", "yellow"); }); </script> </head> <body> <h2>Heading element.</h2> <p>This is the second paragraph.</p> <p>This is the third paragraph.</p> <p>This is the fourth paragraph.</p> </body> </html>
執行上述程式後,<h2> 的 <p> 兄弟元素將被選中,並以黃色背景突出顯示。
示例 2
這裡,我們選擇所有作為 <div> 元素兄弟元素的 <p> 元素:
<html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("div ~ p").css("background-color", "yellow"); }); </script> </head> <body> <div style="border: 2px solid black;">Div element.</div> <h3>Heading element.</h3> <p>This is the third paragraph.</p> <p>This is the fourth paragraph.</p> <h3>Heading element.</h3> </body> </html>
即使父元素 <div> 之後有標題元素,也只會選擇 <p> 元素。
jquery_ref_selectors.htm
廣告