TUTORIAL前端与浏览器FETCHED

jQuery offsetParent() 方法

jQuery offsetParent() 方法 jQuery 遍历方法 实例 设置 <p> 元素的最近的被定位的父元素的背景颜色: $('button').click(function(){ $('p').offsetParent().css('background-color','red'); }..

更新于 2026/7/16

教程详情页默认 noindex。

jQuery offsetParent() 方法

jQuery 遍历方法 jQuery 遍历方法

实例

设置 <p> 元素的最近的被定位的父元素的背景颜色:

$("button").click(function(){
$("p").offsetParent().css("background-color","red");
});

尝试一下 »

定义和用法

offsetParent() 方法返回第一个被定位的父元素。

提示:元素可通过 jQuery 或 CSS 的 position 属性(relative、absolute 或 fixed)来定位。


语法

$(selector).offsetParent()


jQuery 遍历方法 jQuery 遍历方法