Accessing Actual Rendered Font when Undefined in CSS
When accessing the font properties of an element, the JavaScript object.style.fontFamily and object.style.fontSize may return empty values if the corresponding CSS properties are not explicitly set.但是,这并不意味着没有字体就呈现元素。浏览器通常应用默认或继承的样式,该样式定义了实际渲染字体。
检索渲染的字体信息,您可以使用getCompentededStyle方法:For instance:function css(element, property) { return window.getComputedStyle(element, null).getPropertyValue(property); }css(object, 'font-size') // returns '16px'This method returns the computed value of the specified property, even if it was not explicitly set in the CSS.
function css(element, property) { return window.getComputedStyle(element, null).getPropertyValue(property); }
Note:
getComputedStyle is not IE8。live demo: [https://jsfiddle.net/4mxze/](https://jsfiddle.net/4mxze/)
免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3