實例
設置最近的祖先定位元素的背景顏色:
$("button").click(function(){ $("p").offsetParent()
.css("background-color","red"); });
親自試一試
定義和用法
offsetParent() 方法返回最近的祖先定位元素。
定位元素指的是元素的 CSS position 屬性被設置為 relative、absolute 或 fixed 的元素。
可以通過 jQuery 設置 position,或者通過 CSS 的 position 屬性。
語法
$(selector).offsetParent()