javascript - JQuery - Copy dimensions and absolute position of element - Stack Overflow

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

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.

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.

Share Improve this question edited Oct 4, 2019 at 11:41 Vadim Kotov 8,2848 gold badges50 silver badges63 bronze badges asked Jul 11, 2009 at 13:52 RobinRobin
Add a ment  | 

1 Answer 1

Reset to default 4

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745485350a4629740.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信