Getting the calculated div font size

As you know, the font size of an HTML element is either set explicitly as style="font-size:10px", or calculated by the browser in accordance with the rules and properties from the stylesheets and parent properties.

This can be quite a difficult task to calculate the font size value in javascript, since the correct result may depend on classes that do not necessarily exist in the elementName attribute of the element.

Is there a way to get the calculated font size directly, for example div.style['calculated-font-size']? - thanx

+3
source share
1 answer

function elementCurrentStyle (element, styleName) {       if (element.currentStyle) {           var = 0, temp = "", changeCase = false;            (i = 0; < styleName.length; ++)               if (styleName [i]!= '-') {                   temp + = (changeCase? styleName [i].toUpperCase(): styleName [i]);                   changeCase = false;               } else {                   changeCase = true;               }           styleName = temp;           return element.currentStyle [styleName];       } else {           return getComputedStyle (, null).getPropertyValue(styleName);       }   }

alert(elementCurrentStyle(myDiv,"font-size"));

" ", .

,

+6

All Articles