当它没有在CSS中定义时,如何获得实际渲染的字体? [英] How to get the actual rendered font when it's not defined in CSS?
问题描述
当CSS font-face
和 font-size $ c>时,如何获取元素的实际字体面积和字体大小$ c>属性未定义?
How do I get the actual font face and font size of an element when the CSS font-face
and font-size
properties are not defined?
例如,JavaScript代码段
For example, the JavaScript snippet
object.style.fontFamily
不返回任何值。这很明显,假设CSS没有在任何地方应用 object
的样式。但是,当然,某个字体用于渲染文本,可能是系统字体或webbrowser默认字体。
does not return any value. That's pretty obvious, assuming CSS hasn't applied a style to object
anywhere. But, of course, a certain font is used to render the text, probably the system font or the webbrowser default font.
因此,例如,JavaScript可以渲染字体?
So can, for instance, JavaScript get that rendered font?
推荐答案
我建议这个功能:
function css( element, property ) {
return window.getComputedStyle( element, null ).getPropertyValue( property );
}
用法:
css( object, 'font-size' ) // returns '16px' for instance
注意: getComputedStyle
在IE8中无效。
Note: getComputedStyle
doesn't work in IE8.
这篇关于当它没有在CSS中定义时,如何获得实际渲染的字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!