I have a page with two frames, and I need to (via javascript) copy an element and all of its nested elements (it's a ul/li tree) and most importantly it's style from one frame to the other.
I get all the content via assigning innerhtml, and I am able to position the new element in the second frame with dest.style.left and dest.style.top and it works. But I'm trying to get all the style information and nothing's happening.
I'm using getComputedStyle to get the final style for each source element as I loop through each node then and assigning them to the same position in the destination nodelist and nothing happens to visually change the style.
What am I missing?
Have you tried
cloneNode
? It can copy the element and all of its children in one fell swoop. http://www.w3schools.com/dom/met_element_clonenode.aspWell I gave up the original tack of trying to get the [computed] style information out of the source tag, I just never got it to work.
So instead I tried this, and it did work...
First I grabbed the -style- tag from the source frame, then I appendChilded it to the end of the -head- tag of the second frame. For which this proved useful... How do you copy an inline style element in IE?
Then I made a few nested div elements, each having an id or style class I needed to inherit so that the hierarchy matched the first frame. Then I shoved the innerhtml of the source frame's tag that I wanted to copy and then finally appendChilded it to the body of the second frame, and magically, it all worked.
doc.body.appendChild(topd); // voila
With
getComputedStyle
, you can get thecssText
property which will fetch the entire computed style in a CSS string:Unfortunately,
getComputedStyle
isn't supported by Internet Explorer, which usescurrentStyle
instead. Doubly unfortunate is the fact thatcurrentStyle
returns null forcssText
, 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: