How can i get every styles (even inherited) from an element A to an element B ? in javascript or using jquery.
如何从元素A到元素B获得每种样式(甚至是继承的)?在javascript或使用jquery。
let's tell i have an element <p class="foo">...</p>
and i append new element <div />
which would look like the same, except content.
让我们告诉我有一个元素
... 并且我追加新元素
,除了内容外,它们看起来都是一样的。2 个解决方案
#1
41
If you don't care about IE, then you can do this:
如果您不关心IE,那么您可以这样做:
var p = document.getElementById("your_p_id");
var div = document.createElement("div");
div.innerHTML = "your div content";
div.style.cssText = document.defaultView.getComputedStyle(p, "").cssText;
This works for inline, embedded, and inherited styles.
这适用于内联,嵌入和继承的样式。
EDIT: And by "don't care about IE," I of course meant "don't care about anything except Webkit."
编辑:并且通过“不关心IE”,我当然意味着“除了Webkit之外什么都不关心”。
UPDATE: This works in the current versions of Chrome(19), Safari(5), Firefox(12), and IE(9). It also works in older versions of some, such as IE8.
更新:适用于当前版本的Chrome(19),Safari(5),Firefox(12)和IE(9)。它也适用于某些版本的旧版本,例如IE8。
#2
0
Try to copy every CSS-properties like this:
尝试复制每个CSS属性,如下所示:
$("target").css("border", $("source").css("border") );
$("target").css("background", $("source").css("background") );
...
Why not? you can create the dictionary that may consists of all properties.
为什么不?您可以创建可能包含所有属性的字典。
#1
41
If you don't care about IE, then you can do this:
如果您不关心IE,那么您可以这样做:
var p = document.getElementById("your_p_id");
var div = document.createElement("div");
div.innerHTML = "your div content";
div.style.cssText = document.defaultView.getComputedStyle(p, "").cssText;
This works for inline, embedded, and inherited styles.
这适用于内联,嵌入和继承的样式。
EDIT: And by "don't care about IE," I of course meant "don't care about anything except Webkit."
编辑:并且通过“不关心IE”,我当然意味着“除了Webkit之外什么都不关心”。
UPDATE: This works in the current versions of Chrome(19), Safari(5), Firefox(12), and IE(9). It also works in older versions of some, such as IE8.
更新:适用于当前版本的Chrome(19),Safari(5),Firefox(12)和IE(9)。它也适用于某些版本的旧版本,例如IE8。
#2
0
Try to copy every CSS-properties like this:
尝试复制每个CSS属性,如下所示:
$("target").css("border", $("source").css("border") );
$("target").css("background", $("source").css("background") );
...
Why not? you can create the dictionary that may consists of all properties.
为什么不?您可以创建可能包含所有属性的字典。