设置/复制javascript计算的样式从一个元素到另一个

阿迪·达拉奇

所以我试图复制所有应用于一个元素的样式(class / id / tagName / attribute等)。到目前为止,我发现我可以复制元素的计算样式,只是一个问题...可以将其应用于其他元素; /

或以其他方式复制所有样式。

(据我所知:/)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.getPropertyValue(key), computedStyle.getPropertyPriority(key));
  });
}

原始答案发布于13年11月。当时不支持CSS变量。(于2014年7月首次在firefox上进行介绍)

而已!我知道了 :)

我已经看到很多人都看过这个问题,因此下面是更详细,更简洁的代码。

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];

            }   
    }   

};

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将特定的列从一个DataTable复制到另一个

来自分类Dev

将文件从一个目录复制到另一个

来自分类Dev

将数据从一个hbase表复制到另一个

来自分类Dev

将数据从一个表复制到另一个表

来自分类Dev

将元素从一个内存复制到另一个

来自分类Dev

将文件从一个目录复制到另一个目录

来自分类Dev

使用jQuery无法在Firefox中将背景从一个元素复制到另一个元素

来自分类Dev

将内容从一个元素复制到另一个

来自分类Dev

将JS事件从一个元素复制到另一个

来自分类Dev

将消息从一个队列复制到另一个队列

来自分类Dev

将像素从一个图像复制到另一个

来自分类Dev

从一个主机到另一个主机的Ansible复制ssh密钥

来自分类Dev

将2d数组元素从一个复制到另一个

来自分类Dev

在Django中将字段从一个实例复制到另一个

来自分类Dev

将Android Activity从一个应用复制到另一个

来自分类Dev

如何从一个文件复制到另一个?

来自分类Dev

如何计算从一个Hive表复制到另一个的行

来自分类Dev

将几列从一个表复制到另一个

来自分类Dev

将范围从一个容器复制到另一个容器

来自分类Dev

PlistBuddy-从一个plist复制到另一个

来自分类Dev

将条件从一个对象复制到另一个对象

来自分类Dev

将文件从一个拉链复制到另一个?

来自分类Dev

复制从一个表值到另一个VBA带条件

来自分类Dev

将文件从一个拉链复制到另一个?

来自分类Dev

设置/将javascript计算的样式从一个元素复制到另一个元素

来自分类Dev

使用jQuery无法在Firefox中将背景从一个元素复制到另一个元素

来自分类Dev

javascript-设置字符颜色从一个到另一个

来自分类Dev

使用CSS将样式/格式从一个WordPress小部件复制到另一个

来自分类Dev

xquery将文本值从一个元素复制到另一个

Related 相关文章

  1. 1

    将特定的列从一个DataTable复制到另一个

  2. 2

    将文件从一个目录复制到另一个

  3. 3

    将数据从一个hbase表复制到另一个

  4. 4

    将数据从一个表复制到另一个表

  5. 5

    将元素从一个内存复制到另一个

  6. 6

    将文件从一个目录复制到另一个目录

  7. 7

    使用jQuery无法在Firefox中将背景从一个元素复制到另一个元素

  8. 8

    将内容从一个元素复制到另一个

  9. 9

    将JS事件从一个元素复制到另一个

  10. 10

    将消息从一个队列复制到另一个队列

  11. 11

    将像素从一个图像复制到另一个

  12. 12

    从一个主机到另一个主机的Ansible复制ssh密钥

  13. 13

    将2d数组元素从一个复制到另一个

  14. 14

    在Django中将字段从一个实例复制到另一个

  15. 15

    将Android Activity从一个应用复制到另一个

  16. 16

    如何从一个文件复制到另一个?

  17. 17

    如何计算从一个Hive表复制到另一个的行

  18. 18

    将几列从一个表复制到另一个

  19. 19

    将范围从一个容器复制到另一个容器

  20. 20

    PlistBuddy-从一个plist复制到另一个

  21. 21

    将条件从一个对象复制到另一个对象

  22. 22

    将文件从一个拉链复制到另一个?

  23. 23

    复制从一个表值到另一个VBA带条件

  24. 24

    将文件从一个拉链复制到另一个?

  25. 25

    设置/将javascript计算的样式从一个元素复制到另一个元素

  26. 26

    使用jQuery无法在Firefox中将背景从一个元素复制到另一个元素

  27. 27

    javascript-设置字符颜色从一个到另一个

  28. 28

    使用CSS将样式/格式从一个WordPress小部件复制到另一个

  29. 29

    xquery将文本值从一个元素复制到另一个

热门标签

归档