JavaScript   发布时间:2022-04-16  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了将javascript计算样式从一个元素设置为另一个元素大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
所以我试图复制适用于一个元素的所有样式(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);

解决方法

更新:
正如@ icl7126建议的那样,这是一个更短的版本,实际上用于相同的用途.
好的一点是要记住,如果没有预先编译,这段代码将无法在大多数/旧版浏览器上运行.

原创(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.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];

            }   
    }   

};

大佬总结

以上是大佬教程为你收集整理的将javascript计算样式从一个元素设置为另一个元素全部内容,希望文章能够帮你解决将javascript计算样式从一个元素设置为另一个元素所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。