设置/复制javascript计算的样式从一个元素到另一个 [英] Set / Copy javascript computed style from one element to another
问题描述
因此,我试图复制应用于一个元素的所有样式(类/ id / tagName /属性等)。
到目前为止,我发现我可以复制元素的计算样式
只是一个问题...可以将其应用于外部元素; /
So I am trieing to copy all the style that apply on one element ( class / id / tagName / attribute etc. ). So far I found out that I can copy the computed style of an element, Just one problem ... couldend apply it on outher element ;/
或者复制所有样式的另一种方式。
or diffrend way to copy all the style.
(据我所知:/)
http://jsfiddle.net/8KdJd/2/
//queriks mode + minor changes to retrive the computed style
function getCS(el)
{
if (el.currentStyle)
var y = el.currentStyle;
else if (window.getComputedStyle)
var y = document.defaultView.getComputedStyle(el,null);
return y;
}
function setCS(el,cs)
{
if (el.currentStyle)
{
el.currentStyle = cs;
el.style = cs;
}
else if (window.getComputedStyle)
{el.style = cs
}
}
var myLink = document.getElementById('myLink');
var anotherLink = document.getElementById('anotherLink');
var CS_myLink = getCS(myLink);
setCS(anotherLink,CS_myLink);
推荐答案
更新:
如@ icl7126所示,这是几乎相同用法的较短版本。
要记住,如果不进行预编译,此代码将无法在大多数/旧版浏览器上运行。
Update: As @icl7126 suggested, here is a shorter version for practically the same usage. good thing to remember that this code would not run on most/older browser if not pre-compiled.
原始版本(ES 2017):
Original (ES 2017):
function copyNodeStyle(sourceNode, targetNode) {
const computedStyle = window.getComputedStyle(sourceNode);
Array.from(computedStyle).forEach(key => targetNode.style.setProperty(key, computedStyle.getPropertyValue(key), computedStyle.getPropertyPriority(key)))
}
预编译(ES 5):
function copyNodeStyle(sourceNode, targetNode) {
var computedStyle = window.getComputedStyle(sourceNode);
Array.from(computedStyle).forEach(function (key) {
return targetNode.style.setProperty(key, computedStyle.getPropertyValue(key), computedStyle.getPropertyPriority(key));
});
}
#
原始答案发布于13年11月。当时不支持CSS变量。 (于2014年7月在Firefox上首次引入)
就是这样!我明白了:)
Thats it! I got it :)
我看到很多人都在看这个问题,
所以下面是更详细,更简洁的代码。
Iv'e seen that lots of people view this question, So below is more detailed and clean code.
var copyComputedStyle = function(from,to){
var computed_style_object = false;
//trying to figure out which style object we need to use depense on the browser support
//so we try until we have one
computed_style_object = from.currentStyle || document.defaultView.getComputedStyle(from,null);
//if the browser dose not support both methods we will return null
if(!computed_style_object) return null;
var stylePropertyValid = function(name,value){
//checking that the value is not a undefined
return typeof value !== 'undefined' &&
//checking that the value is not a object
typeof value !== 'object' &&
//checking that the value is not a function
typeof value !== 'function' &&
//checking that we dosent have empty string
value.length > 0 &&
//checking that the property is not int index ( happens on some browser
value != parseInt(value)
};
//we iterating the computed style object and compy the style props and the values
for(property in computed_style_object)
{
//checking if the property and value we get are valid sinse browser have different implementations
if(stylePropertyValid(property,computed_style_object[property]))
{
//applying the style property to the target element
to.style[property] = computed_style_object[property];
}
}
};
这篇关于设置/复制javascript计算的样式从一个元素到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!