jQuery offsetParent() 方法
实例
设置 <p> 元素最近位置的父元素的背景色:
<!DOCTYPE html><html><head><script src="https://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script><script>$(document).ready(function(){$("button").click(function(){$("p").offsetParent().css("background-color", "red");});});</script></head><body><button>设置背景色</button><div style="border:1px solid black;width:70%;position:absolute;left:10px;top:50px"><div style="border:1px solid black;margin:50px;background-color:yellow"><p>单击按钮设置此段落的第一个定位父元素的背景色</p></div></div></body></html>
定义与用法
offsetParent() 方法返回第一个定位的父元素。
提示:可以使用 jQuery 或 CSS position 属性(
relative(相对), absolute(绝对), 或 fixed(固定))定位元素。语法
$(selector).offsetParent()