jQuery 使用jQuery以像素为单位检测字体大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15154601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:29:27  来源:igfitidea点击:

Detect font size in pixels using jQuery

javascriptjquery

提问by Michal

How can I detect font size using jQuery? If I am using emand i need to detect font size in pixels

如何使用 jQuery 检测字体大小?如果我正在使用em并且我需要以像素为单位检测字体大小

<h1 style="font-size: 5em">Size of font in pixels?</h1>

回答by James

http://jsfiddle.net/g9uKq/

http://jsfiddle.net/g9uKq/

I made you a fiddle just to be clear.

为了清楚起见,我让你成为了小提琴。

var size = $("h1").css('font-size');

回答by Sergio

This should work:

这应该有效:

parseInt($("h1").css("font-size"))

回答by David Omid

Have you tried using this method?

您是否尝试过使用这种方法?

var size = $(element).css('font-size');

回答by EnterJQ

Try this,

尝试这个,

function em(input) {
 var emSize = parseFloat($("h1").css("font-size"));
 return (emSize * input);
}

回答by bipen

use css()to get the font-size... this gives the size in px... so divide it by 16 should give you the value in em. try this

用于css()获取字体大小...这给出了以 px 为单位的大小...所以将它除以 16 应该给你em. 尝试这个

 var sizeinem=parseFloat($('h1').css('font-size')) / 16;
 alert(sizeinem);

fiddle here

在这里摆弄