With getComputedStyle
, you can get the cssText
property which will fetch the entire computed style in a CSS string:
var completeStyle = window.getComputedStyle(element1, null).cssText;
element2.style.cssText = completeStyle;
Unfortunately, getComputedStyle
isn't supported by Internet Explorer, which uses currentStyle
instead. Doubly unfortunate is the fact that currentStyle
returns null for cssText
, so the same method cannot be applied to IE. I'll try and figure something out for you, if nobody beats me to it :-)
I thought about it and you could emulate the above in IE using a for...in
statement:
var completeStyle = "";
if ("getComputedStyle" in window)
completeStyle = window.getComputedStyle(element1, null).cssText;
else
{
var elStyle = element1.currentStyle;
for (var k in elStyle) { completeStyle += k + ":" + elStyle[k] + ";"; }
}
element2.style.cssText = completeStyle;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…