JQuery - 复制元素的维度和绝对位置

时间:2022-11-18 18:12:22

I'm trying to copy a element's dimensions and the position(relative to the document) onto another element.

我正在尝试将元素的尺寸和位置(相对于文档)复制到另一个元素上。

Ex:

  var SelectedElement = $("div#MyTargetElement");

  // The CopiedButEmpty element is a div with absolute position that is meant to hover/float above the selected element.
  $("div#CopiedButEmpty").css("width", SelectedElement.width()).css("height", SelectedElement.height())
    .css("left", SelectedElement.offset().left).css("top", SelectedElement.offset().top)
    .css("marginTop", SelectedElement.css("marginTop")).css("marginLeft", SelectedElement.css("marginLeft"))
    .css("marginRight", SelectedElement.css("marginRight")).css("marginBottom", SelectedElement.css("marginBottom"))
    .css("paddingLeft", SelectedElement.css("paddingLeft")).css("paddingTop", SelectedElement.css("paddingTop"))
    .css("paddingRight", SelectedElement.css("paddingRight")).css("paddingBottom", SelectedElement.css("paddingBottom"));

But in some cases, it still does not give me the correct position. Am I missing something? Is there any way to easily copy an elements position(relative to the document), so my element "CopiedButEmpty" can hover the element.

但在某些情况下,它仍然没有给我正确的立场。我错过了什么吗?有没有办法轻松复制元素位置(相对于文档),所以我的元素“CopiedButEmpty”可以悬停元素。

Thanks in advance!

提前致谢!

1 个解决方案

#1


I wonder why you didn't chose to use jquery clone().

我想知道为什么你没有选择使用jquery clone()。

#1


I wonder why you didn't chose to use jquery clone().

我想知道为什么你没有选择使用jquery clone()。