TUTORIAL前端与浏览器FETCHED
jQuery offsetParent() 方法
jQuery offsetParent() 方法 jQuery HTML/CSS 方法 实例 设置 <p> 元素的最近定位的祖先元素的背景颜色: [mycode3 type='js'] $('button').click(function(){ $('p').offsetParent().css('background-col..
更新于 2026/7/16
教程详情页默认 noindex。
jQuery offsetParent() 方法
实例
设置 <p> 元素的最近定位的祖先元素的背景颜色:
$("button").click(function(){
$("p").offsetParent().css("background-color","red");
});
尝试一下 »
定义和用法
offsetParent() 方法返回第一个定位的祖先元素。
提示:通过 jQuery 或 CSS 的 position 属性(relative、absolute 或 fixed)对元素进行定位。
语法
$(selector).offsetParent()
jQuery HTML/CSS 方法