简体   繁体   English

如何快速找到绝对定位的子项的相对定位的HTML父元素?

[英]How can I quickly find the relatively positioned HTML parent element of an absolutely positioned child?

I am working with the following code: 我正在使用以下代码:

<div>
    <div>
        <div>
            <div>
                <div>
                    <div>
                        ...
                            <div class="absolute">Lorem ispum</div>
                        ...
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

.absolute {
  position: absolute;
}

Using Chrome's DevTools, I'd like to be able to quickly find which parent <div> element has position: relative , so I can know which parent element is determining the origin point of my absolutely positioned element. 使用Chrome的DevTools,我希望能够快速找到哪个父<div>元素具有position: relative ,所以我可以知道哪个父元素正在确定我绝对定位元素的原点。 I can manually inspect each of the divs to find the first parent that is set to relative but I'm working with a deeply nested tree, so I'm wondering if there is a faster way to find the parent I'm trying to find. 我可以手动检查每个div以找到设置为relative的第一个父级,但我正在使用深度嵌套的树,所以我想知道是否有更快的方法来查找我正在尝试查找的父级。 A Javascript snippet for the console would be an example of a suitable solution. 控制台的Javascript代码段将是合适解决方案的示例。

HTMLElement.offsetParent is just designed to do this. HTMLElement.offsetParent旨在实现此目的。

See this document on MDN . 请参阅MDN上的此文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React应用,绝对定位的子对象未相对于其相对定位的父对象定位(在Chrome中) - React app, absolutely positioned child not positioned relative to its relatively positioned parent (in Chrome) 如何将绝对定位的div水平居中于其相对定位的父级? - How to horizontally center an absolutely positioned div over its relatively positioned parent? 在父容器的动画期间,绝对定位的子元素被剪裁 - Absolutely positioned child element gets clipped during animation of parent container 如何让一个相对定位的孩子获得所有父母的身高 - How to make a relatively positioned child to get all parent's height 您可以滚动到 html 中相对定位的元素的锚点吗? - Can you scroll to an anchor in html of an element that is relatively positioned? 找到绝对定位元素的参考框架 - Find absolutely positioned element's frame of reference 相对于绝对:获得相对定位的元素(如果绝对定位)将具有的位置? - Relative to absolute: get the position that a relatively positioned element would have if it was absolutely positioned? 我们可以为绝对定位的元素指定一个定位元素吗? - Can we specify an positioned element for absolutely positioned elements? 绝对定位元素的宽度 - Width of absolutely positioned element 如何将相对定位的元素居中与jquery? - How to center a relatively positioned element with jquery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM