所以我试图复制适用于一个元素的所有样式(class / id / tagName / attribute等)。 到目前为止,我发现我可以复制元素的计算样式, 只有一个问题... couldend将它应用于其他元素; /
或diffrend复制所有样式的方式。
(这是我得到的:/) 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);
答案 0 :(得分:14)
更新: 正如@ icl7126建议的那样,这里有一个更短的版本,实际上用于相同的用法。 好的一点是要记住,如果没有预先编译,这段代码就无法在大多数/旧版浏览器上运行。
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));
});
}
多数民众赞成!我明白了:)
我看到很多人都看到这个问题, 所以下面是更详细和干净的代码。
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];
}
}
};
答案 1 :(得分:4)
使用setProperty
,getPropertyValue
和getPropertyPriority
的简短解决方案。
function copyNodeStyle(sourceNode, targetNode) {
const computedStyle = window.getComputedStyle(sourceNode);
Array.from(computedStyle).forEach(key => targetNode.style.setProperty(key, computedStyle.getPropertyValue(key), computedStyle.getPropertyPriority(key)))
}
有用的MDN文档:getComputedStyle和CSSStyleDeclaration
答案 2 :(得分:0)
计算的样式不会保留优先级信息(jsonChain
),因此... !important
将不会返回样式优先级,因此上面的getComputedStyle()
(在可接受的答案中)将不起作用-它将始终返回computedStyle.getPropertyPriority(key)
。
如果要确保计算的样式始终具有优先级,则显式设置优先级:
''