本文介绍了JQuery - 复制元素的尺寸和绝对位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图将元素的尺寸和相对于文档的位置复制到另一个元素上。
I'm trying to copy a element's dimensions and the position(relative to the document) onto another element.
例如:
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"));
但在某些情况下,它仍然不能给我正确的位置。我缺少什么?有任何方法可以轻松地复制元素的位置(相对于文档),所以我的元素CopiedButEmpty可以悬停元素。
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.
提前感谢! >
Thanks in advance!
推荐答案
我不知道为什么你没有选择使用jquery 。
I wonder why you didn't chose to use jquery clone().
这篇关于JQuery - 复制元素的尺寸和绝对位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!