您的位置:首页 > Web前端 > JavaScript

javascript克隆元素样式的实现代码

2011-10-23 00:00 591 查看
/** 
* 克隆元素样式 
* @param {HTMLElement} 被克隆的元素 
* @param {Boolean} 是否启用缓存(默认true) 
* @return {String} css类名 
*/ 
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(doc.createTextNode(content)); 
}, 
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));

演示:





cloneStyle

/**
* 克隆元素样式
* @author tang bin
* @version 0.1
* @see http://www.planeart.cn/?p=1499
* @param {HTMLElement} 被克隆的元素
* @param {Boolean} 是否启用缓存(默认true)
* @return {String} css类名
*/
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(doc.createTextNode(content));
},
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));


div { width:150px; margin:20px; background:#FBFCFD; border:1px solid #D0DCE8; text-align:center; 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; }



克隆div的最终样式到span
我是DIV标签
我是SPAN标签

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: