首页 > 技术文章 > 自定义 获取元素当前样式 函数

tianxiaxuange 2018-10-29 22:42 原文

嗯哼。不多说,直接上代码了。

  • // 自定义 获取元素当前样式    兼容性封装 Test Already.
    function getCurrentStyle(obj, name){
        if(window.getComputedStyle){
            // 大多浏览器支持, IE8 及以下不支持
            return window.getComputedStyle(obj, null)[name];    // 伪类 null
        }else{
            // 只有 IE 浏览器支持
            return obj.currentStyle[name];    // 当获取 left 这样的属性时,可能返回 auto
        }
    }

     


推荐阅读