Created
January 20, 2015 23:51
-
-
Save jennz0r/1078a71f1e6b763a0890 to your computer and use it in GitHub Desktop.
Syntax Highlighting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function cloneElementWithStyles(element) { | |
var clonedEl = $(document.createElement(element.tagName)); | |
// CSS styles to copy over from the generated node. | |
var stylesToCopy = ['font', 'color']; | |
// Copy css styles. | |
clonedEl.attr('style', getComputedStyles(stylesToCopy, element)); | |
// Clone children. | |
$(element).contents().each(function() { | |
if (this.nodeType === Node.ELEMENT_NODE) { | |
clonedEl.append(cloneElementWithStyles(this)); | |
} else { | |
clonedEl.append($(this).clone()); | |
} | |
}); | |
return clonedEl; | |
} | |
function getComputedStyles(stylesToCopy, element) { | |
var cssText = stylesToCopy.reduce(function(cssText, property) { | |
cssText += property + ':' + window.getComputedStyle(element)[property] + ';'; | |
return cssText; | |
}, ''); | |
return cssText; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment