问题描述
我有一些具有以下近似结构和定位的HTML:
I have some HTML with the following approximate structure and positioning:
<div class="grand-parent" style="position: absolute">
<div class="parent" style="position: relative">
<div class="child"></div>
</div>
</div>
在我的jQuery小部件中,我正在尝试插入位于偏移父级内的元素小部件所针对的元素。为此,我基本上有这样的代码:
In my jQuery widget, I'm trying to insert an element that is located inside the "offset parent" of the element targeted by the widget. To do so, I essentially have code like this:
var targetElement = $('.child');
$('<div/>').appendTo(targetElement.offsetParent());
不幸的是,该元素似乎是作为的孩子插入的.grand- parent
而不是 parent
。我对 offsetParent()
(以及文档似乎支持这个)的理解是 offsetParent()
应该返回 .parent
因为它位于相对位置。我对offsetParent的理解是不正确的,还是jQuery存在问题(我使用的是1.4.1)。
Unfortunately, the element appears to be inserted as a child of .grand-parent
instead of parent
. My understanding of offsetParent()
(and the documentation seems to back this) is that offsetParent()
should return .parent
because it is positioned relative. Is my understanding of offsetParent incorrect, or is there a problem with jQuery (I'm using 1.4.1).
推荐答案
根据 https://developer.mozilla.org/en/DOM/element.offsetParent, offsetParent
如果未显示父级( display:none
),则无法正常工作。就我而言,容器元素就是这样。
According to https://developer.mozilla.org/en/DOM/element.offsetParent, offsetParent
doesn't work as I had expected if the parent is not displayed (display: none
). In my case, the container element is just that.
这篇关于jQuery中的offsetParent()没有返回预期的相对位置祖先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!