css获取实时样式

function getStyle(elem,styleName){
    if(elem.style[styleName]){//内联样式
           return elem.style[styleName];
    }
    else if(elem.currentStyle){//IE
           return elem.currentStyle[styleName];
    }
    else if(document.defaultView && document.defaultView.getComputedStyle){//DOM
           styleName = styleName.replace(/([A-Z])/g,'-$1').toLowerCase();
           var s = document.defaultView.getComputedStyle(elem,'');
           return s&&s.getPropertyValue(styleName);
    }
     else if(window.getComputedStyle){
           styleName = styleName.replace(/([A-Z])/g,'-$1').toLowerCase();
           var s = window.getComputedStyle(elem,'');
           return s&&s.getPropertyValue(styleName);
     }
     else{//other,for example, Safari
           return null;
     }
}

 

posted @ 2015-09-10 15:04  全玉  阅读(443)  评论(0编辑  收藏  举报