JavaScript cloning element style implementation code _javascript tips

Source: Internet
Author: User
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]
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.