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