在JavaScript中获取计算的font-family [英] Get computed font-family in JavaScript
问题描述
这是此问题的后续动作。
我正在使用的组件CKEditor ,一个调整版本的字体下拉菜单,总是显示当前选择的字体系列/大小值,无论它们在何处被定义,使用 computedStyle
和礼服。
正如你可以在另一个问题中看到的,确定字体大小现在是跨浏览器。现在我无法使用 fontFamily
属性。我的通用计算样式函数只返回定义的完整字体字符串,例如
Times New Roman,Georgia,Serif
我需要的是,为了匹配字体系列下拉列表中的条目,我要检查的DOM元素的实际字体中的strong>固定字体名称。
至少对于最常见的网络字体,可以这样做。
我不认为有任何方法直接做,但Lalit帕特尔想出了一个聪明的技术,创建一个元素与一些字母,并从元素的宽度猜测字体。 / p>
请参阅 http:/ /www.lalit.org/lab/javascript-css-font-detect
This is a followup to this question.
I am working on a component for the CKEditor, a tweaked version of the font drop-down menus that always display the currently selected font family / size values, no matter where they were defined, using computedStyle
and consorts.
As you can see in the other question, determining the font size works cross-browser now. Now I am having trouble working with the fontFamily
attribute. My generic "computed style" function returns only the full font string that was defined, e.g.
Times New Roman, Georgia, Serif
What I need, in order to match the setting against the entries in the font family dropdown, is a fixed font name of the actual font of the DOM element I am checking.
Can this be done somehow, at least for the most common web fonts?
I don’t think there are any methods to do it directly, but Lalit Patel came up with a clever technique that creates an element with some letters in it, and guesses the font from the width of the element.
See http://www.lalit.org/lab/javascript-css-font-detect
这篇关于在JavaScript中获取计算的font-family的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!