getComputedStyle与currentStyle获取样式

轩陌

分类: JavaScript 2943 3

getComputedStyle与currentStyle的区别:

    同为获取元素的样式,currentStyle适用于IE6 7 8 的浏览器,getComputedStyle适用于除IE6 7 8 的浏览器,在写代码的时候需要写一下兼容。


window.onload = function() {
    var oBox = document.getElementById('box');
    alert(getStyle(oBox, 'width'));

    function getStyle(obj, attr) {
        //简化版
        return obj.currentStyle
            ? obj.currentStyle[attr]
            : getComputedStyle(obj)[attr];
    }
}
  • 3人 Love
  • 0人 Haha
  • 0人 Wow
  • 0人 Sad
  • 0人 Angry
JavaScript、前端笔记

作者简介: 轩陌

打赏

生命的意义在于折腾,一直努力成长中,期待梦想实现的那刻。

共 3 条评论关于 “getComputedStyle与currentStyle获取样式”

Loading...