gpt4 book ai didi

javascript - 如何找到元素在其父容器中的位置?

转载 作者:行者123 更新时间:2023-11-28 00:03:30 31 4
gpt4 key购买 nike

考虑下图:

enter image description here

我想计算子元素在其直接父元素中的垂直偏移量。请注意,子元素有边距(用浅蓝色表示)。

jQuery 提供 .position() ,但不幸的是,该方法计算偏移量:

"...relative to the offset parent."

在这种情况下,父元素没有position设置为relative,不幸的是,这无法更改。

尽管如此,有什么方法可以获取子元素相对于其直接父元素的偏移量吗?

最佳答案

你可以这样做

$(this).offset().top - $(this).parent().offset().top

关于javascript - 如何找到元素在其父容器中的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19870184/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com