javascript – 从计算样式(currentStyle)继承的字体大小的IE读取不正确

我在这里放了一个小测试案例:

http://jsfiddle.net/D4sLk/2/

基本上我设置了以下字体大小:

> *(一切):12px
>容器:20px
>测试元素:继承

DOM层次结构是:container>测试元素.

在IE9中,使用testEl.currentStyle.fontSize将字体大小报告为12px,但显示为20px.在Chrome和FF中看起来很好.

这个问题有没有解决方法?或者我做过一些非常愚蠢的事情?

最佳答案 尝试使用font-size:1em而不是使用inherit.

原因是因为我发现继承似乎在IE中存在问题.当我查看IE9时,它呈现正常,但由于某种原因,testEl.currentStyle.fontSize和$(testEl).css(‘font-size’)也返回12px.

我已经读过在IE8中使用font-size:inherit,你需要指定一个!DOCTYPE,但它在IE9(http://www.w3schools.com/cssref/pr_font_font-size.asp)中应该没问题.出于某种原因,testEl.currentStyle.fontSize和$(testEl).css(‘font-size’)没有在IE9中获取正确的值.

当您将font-size设置为1em时,您将其大小调整为父字体大小的100%,在这种情况下,结果为20px.从http://www.w3schools.com/cssref/css_units.asp开始:

1em is equal to the current font size. 2em means 2 times the size of the current font. E.g., if an element is displayed with a font of 12 pt, then ‘2em’ is 24 pt. The ’em’ is a very useful unit in CSS, since it can adapt automatically to the font that the reader uses

因此,computedStyle.fontSize和$(testEl).css(‘font-size’)都应返回20px.

希望这可以帮助!

点赞