10 09/2014

get current style of element

最后更新: Wed Sep 10 2014 12:37:50 GMT+0800

IE use currentStyle.And other modern browser use

window.getComputedStyle(el).getPropertyValue(cssPropertyName)

function getStyle(el,cssprop){
 if (el.currentStyle) {//ie
         cssArr=cssprop.split('-');//change font-size to fontSize etc.
        for(i=1;i<cssArr.length;i++){
            cssArr[i]=cssArr[i].substr(0,1).toUpperCase()+cssArr[i].substr(1)
        }
        cssprop=cssArr.join('');
      return el.currentStyle[cssprop];
    }else if (window.getComputedStyle) {//modern browser  
      return window.getComputedStyle(el).getPropertyValue(cssprop)
     }else {//try and get inline style
      return el.style[cssprop]
    }
}

Demo of get style

Be Careful aslways use cssprop fontSize like font-size!