如何使用 JavaScript 或 JQuery 获取默认字体大小(以像素为单位)?
如您所知,em 是一种相对字体测量值,其中 1 em 等于默认字体大小中字母“M”的高度。使用它的一个优点是您可以调整文本大小。
但是如何使用 JavaScript 或 JQuery 获取当前环境的默认字体大小(以像素为单位)?
问候,
As you know em is a relative font measurement where one em is equal to the height of the letter "M" in the default font size. An advantage in using it is because you will be able to resize the text.
But how can i get default font size of current environment (in pixels) by using JavaScript or JQuery ?
regards,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
在某些情况下这可能很有用 -
alert(getDefaultFontSize())
There are a couple of situations this can be useful-
alert(getDefaultFontSize())
可以使用以下代码行来完成:
window.getComputedStyle(document.body)
- 获取 body 的所有样式getPropertyValue('font-size')
- 到获取字体大小的字符串值,例如:(16px)match(/\d+/)[0])
- 仅获取数字部分,例如:< em>(16) - 字符串Number(...)
- 将数字部分转换为数字,例如:(16) - 数字It can be done using this line of code:
window.getComputedStyle(document.body)
- to get all the styles for bodygetPropertyValue('font-size')
- to get a string value of font-size, example: (16px)match(/\d+/)[0])
- to get only a number part, example: (16) - stringNumber(...)
- to convert number part into a number, example: (16) - number我相信 M 原则是一个神话。至少来自 http://www.w3.org/TR/CSS21 的以下文档/syndata.html证明基于M原理的计算过于复杂且不必要。
根据本文档,以下内容正确。
没有祖先放大率,1em 正好等于像素字体大小。
没有
由于使用ems和百分比的祖先放大以明确定义的方式工作,一个简单的循环将计算精确的字体大小,假设:没有CSS;并且某些祖先将其字体大小设置为绝对单位。
由于ems测量宽度,你总是可以通过创建一个1000ems长的div并将其client-Width属性除以1000来计算精确的像素字体大小。我似乎记得ems被截断到最接近的千分之一,所以你需要1000 ems 以避免像素结果的错误截断。
您可能可以创建一种 M 原则失败的字体,因为 em 是基于字体大小属性而不是实际字体。假设您有一种奇怪的字体,其中 M 是其他字符大小的 1/3,并且您的字体大小为 10 像素。我认为字体大小是最大字符高度的保证,因此 M 不会是 10 像素,所有其他字符不会是 30 像素。
I believe the M-principle is a myth. At the very least the following documentation from http://www.w3.org/TR/CSS21/syndata.html proves that calculations based on the M-principle are overly complicated and unnecessary.
From this documentation the following are true.
Without ancestor magnification, 1em is exactly equal to the pixel font size.
Since ancestor magnification with ems and percent works in well defined ways a simple loop will calculate exact font sizes, assuming: no C.S.S; and some ancestor has it's font size set in absolute units.
Since ems measure width you can always compute the exact pixel font size by creating a div that is 1000 ems long and dividing its client-Width property by 1000. I seem to recall ems are truncated to the nearest thousandth, so you need 1000 ems to avoid an erroneous truncation of the pixel result.
You probably can create a font where the M-principle fails since em is based on the font-size attribute not on the actual font. Suppose you have a weird font where M is 1/3 the size of the other characters and you have a font size of 10 pixels. I kind of think the font-size is a guarantee of maximal character height and so the M will not be 10 pixels and all other characters 30 pixels.
如果您使用
rem
作为单位,则默认的“font-size”需要从获取,而不是从 body 或其他。
If you use
rem
as unit the default 'font-size' need take from<html>
, not from body or other.我过去见过/使用过的一个技巧是渲染一些文本(例如 50 个任意字符,或 Ms),然后测量渲染容器的大小并进行数学计算以计算出单个字符的高度和宽度。这就是你想要的吗?您可以在后台进行渲染,这样用户就看不到它。
One trick I've seen/used in the past is to render some text (say 50 arbitrary characters, or Ms) and then measure the size of the rendered container and do the math to figure out the height and width of a single character. Is this what you're going for? You can do the rendering in the background so the user doesn't see it.
使用 jQuery(假设您需要特定元素的字体大小):
var originalFontSize = $('#your_element_id_here').css('font-size')
;如果您使用 Prototype 以及 jQuery,则需要使用 jQuery 前缀而不是美元符号:
这将以字符串形式返回值,如下所示:16px。
Using jQuery (assuming that you want the font size of a specific element):
var originalFontSize = $('#your_element_id_here').css('font-size')
;If you're using Prototype as well as jQuery, you'll want to use the jQuery prefix instead of the dollar sign:
This will return the value as a string like so: 16px.
尽管获取默认字体大小似乎是一个好主意 - 如果要设置页面布局等,那么让用户处理它可能是更安全的做法。
如果他们的字体较大,那是因为他们是盲人,反之亦然。我建议设置默认值并“推荐”分辨率/尺寸。除非你的应用程序依赖它——让用户处理它。
Although it seems like getting the default font size might be a good idea -- if it's to set the layout of your pages and such, it's probably a safer practice to just let the user handle that.
If they have their font size larger - it's because they're blind and vice versa. I'd recommend setting your defaults and 'recommending' a resolution/size. Unless your app depends on it -- let the user handle it.
这在 FF 中有效。
This works in FF.
我认为这就是您正在寻找的:
*如果 body 在代码中的其他任何地方(也许在某些 css 中)有不同的 fontSize 减法,则该函数将返回该值。例如:
上面将返回 20px 的 fontSize 值。
Fully tested and works for me (chrome 22.0.1229.94 m and firefox 13.01).
I think this is what you're looking for:
*If body has a different fontSize decleration anywhere else in the code (perhaps in some css) the function will return that value. For example:
The above will return a fontSize value of 20px.
Fully tested and works for me (chrome 22.0.1229.94 m and firefox 13.01).