开发者

Getting the calculated font-size of a div

开发者 https://www.devze.com 2023-02-15 22:07 出处:网络
As we know, the font-size of a HTML-element 开发者_开发技巧is either set explicitly like style=\"font-size:10px\" or calculated by the browser according to rules and properties from style sheets and p

As we know, the font-size of a HTML-element 开发者_开发技巧is either set explicitly like style="font-size:10px" or calculated by the browser according to rules and properties from style sheets and parent properties.

It could be a rather complex task to calculate the font-size value in javascript as the correct result might depend on classes that do not necessarily exist in the elements className attribute.

Is there a way I can get the calculated font-size directly, like div.style['calculated-font-size'] ? - thanx


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

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

I've describe this "getting computed style" issue few weeks ago here.

cheers,

0

精彩评论

暂无评论...
验证码 换一张
取 消