Copy Code code as follows:
/**
* Clone element style
* @param {HtmlElement} cloned elements
* @param {Boolean} Whether caching is enabled (default true)
* @return {String} CSS class name
*/
var Clonestyle = (function (DOC) {
var Rstyle =/^ (number|string) $/,
Clonename = ' ${clonename} ',
SData = {},
Addheadstyle = function (content) {
var style = Sdata[doc];
if (!style) {
style = Sdata[doc] = doc.createelement (' style ');
Doc.getelementsbytagname (' head ') [0].appendchild (style);
};
Style.stylesheet && (Style.styleSheet.cssText + + content) | | Style.appendchild (content) (Doc.createtextnode);
},
GetStyle = ' getcomputedstyle ' in window? function (elem, name) {
Return getComputedStyle (Elem, NULL) [name];
}: Function (Elem, name) {
return Elem.currentstyle[name];
};
return function (source, cache) {
if (!cache && source[clonename]) return source[clonename];
var className, name,
Csstext = [],
Sstyle = Source.style;
for (name in Sstyle) {
val = getstyle (source, name);
if (Val!== ' && rstyle.test (typeof val)) {
name = Name.replace ([A-z])/g, "-$1"). toLowerCase ();
Csstext.push (name);
Csstext.push (': ');
Csstext.push (Val);
Csstext.push (';');
};
};
Csstext = Csstext.join (");
Source[clonename] = ClassName = ' clone ' + (new Date). GetTime ();
Addheadstyle ('. ' + ClassName + ' {' + csstext + '} ');
return className;
};
} (document));
Demonstrate:
<! DOCTYPE html> <ptml> <pead> <meta http-equiv= "Content-type" content= "text/html"; charset=gb2312 "> <title>cloneStyle</title> <script type=" Text/javascript ">/** * cloning element style * @autho R Tang Bin * @version 0.1 * @see http://www.planeart.cn/?p=1499 * @param {htmlelement} The cloned element * @param {Boolean} is enabled for slow Save (Default True) * @return {String} CSS class name */var Clonestyle = (function (doc) {var rstyle =/^ (number|string) $/, Clonename = ' ${clonename} ', SData = {}, Addheadstyle = function (content) {var style = Sdata[doc]; if (!style) {style = Sdata[doc] = doc.createelement (' style '); Doc.getelementsbytagname (' head ') [0].appendchild (style); }; Style.stylesheet && (Style.styleSheet.cssText + + content) | | Style.appendchild (content) (Doc.createtextnode); }, GetStyle = ' getcomputedstyle ' in window? function (elem, name) {return getComputedStyle (elem, NULL) [name]; }: Function (Elem, name) {return elem.currentstyle[name]; }; Returnfunction (source, cache) {if (!cache && source[clonename]) return source[clonename]; var className, name, Csstext = [], Sstyle = Source.style; for (name in Sstyle) {val = getstyle (source, name); if (Val!== ' && rstyle.test (typeof val)) {name = Name.replace (/([A-z])/g, "-$1"). toLowerCase (); Csstext.push (name); Csstext.push (': '); Csstext.push (Val); Csstext.push (';'); }; }; Csstext = Csstext.join ("); Source[clonename] = ClassName = ' clone ' + (new Date). GetTime (); Addheadstyle ('. ' + ClassName + ' {' + csstext + '} '); return className; }; } (document)); </script> <style> Div {width:150px; margin:20px background: #FBFCFD; border:1px solid #D0DCE8; text-align:c Enter Line-height:3em; Font-size:1.5em; }. skin {-webkit-border-radius:5px;-moz-border-radius:5px; border-radius:5px;} #div {width:300px; height:300px;} span {border:1px solid #D0DCE8; color: #F00;} </style> </pead> <body> <button onclick= "docuMent.getelementbyid (' span '). ClassName = Clonestyle (document.getElementById (' div ')); > Clone div final style to span</button> <div id= "div" class= "skin" > I'm div tag </div> <span id= "span" > I am span label </span> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]