I have tried “window.getComputedStyle” and “currentStyle”,but it not work except the chrome.
Please look my demo below firstly,thanks.
http://www.learning.fancyboy.net/javascript/cloneStyles.html
The code is:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>clone style</title>
<style>
*{margin:0;padding:0;}
#text1{width:200px;height:50px;border:1px solid red;color:#ccc;line-height:50px;padding:5px;margin:5px;}
</style>
</head>
<body>
<div>
<input type="text" id="text1" value="origin" />
<input type="text" id="text2" value="clone" />
</div>
<script>
var
text1=document.getElementById("text1"),
text2=document.getElementById("text2"),
cssObj,
sCssText="";
if(!!window.getComputedStyle){
cssObj=window.getComputedStyle(text1,null);
sCssText=cssObj.cssText;
}
else{
cssObj=text1.currentStyle;
for(var k in cssObj){
sCssText+=k+":"+cssObj[k]+";";
}
}
text2.style.cssText=sCssText;
</script>
</body>
</html>
Any idea?
If possible I would try to do something like this:
If that isn’t a possibility then you could use this jQuery function (from http://upshots.org/javascript/jquery-copy-style-copycss):
And the helper to copy (all styles of @source are passed to invoker):