
- 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 offsetParent() 方法
jQuery 中的offsetParent() 方法用於獲取所選元素最近的已定位祖先元素。
您可以使用 jQuery 或利用 CSS position 屬性(例如 relative、absolute 或 fixed 定位)來定位元素。
語法
以下是 jQuery 中 offsetParent() 方法的語法:
$(selector).offsetParent()
引數
此方法不接受任何引數。
示例
在下面的示例中,我們使用 offsetParent() 方法來查詢<p> 元素最近的已定位父元素:
<html> <head> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ // Find the offset parent of the p element const offsetParent = $("p").offsetParent(); // Check if the offset parent exists if(offsetParent !== null) { // Set the background color of the offset parent to green offsetParent.css("background-color", "green"); alert("offset parent found. It will now be highlighted with green color."); } else { alert("No offset parent found."); } }); }); </script> </head> <body> <div style="border:1px solid black; width: 30%; position:absolute; left:10px; top:50px" > <div style="border:1px solid black; margin:50px; background-color:yellow"> <p>Click on the above button to find and set background color for the first positioned parent element of this paragraph.</p> </div> </div> <button>Click here!</button> </body> </html>
執行上述程式後,如果我們點選按鈕,該方法會找出<p> 元素的 offsetParent,發出警報,並將背景顏色設定為綠色。
jquery_ref_traversing.htm
廣告