如何获取由css设置的元素的字体大小 [英] How can I get the font size of an element as it was set by css

查看:590
本文介绍了如何获取由css设置的元素的字体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个简单的jQuery来改变元素的字体大小一定的百分比。我遇到的问题是,当我使用jQuery的$('#el')。css('font-size')获取大小时,它总是返回一个像素值,即使设置为em。当我使用Javscript的el.style.font-size属性时,它不会返回一个值,直到同一个属性显式设置。

I'm writing a simple jQuery that to change the font size of an element by a certain percentage. The problem I'm having is that when I get the size with jQuery's $('#el').css('font-size') it always returns a pixel value, even when set with em's. When I use Javscript's el.style.font-size property, it won't return a value until one has been explicitly set by the same property.

有没有一些方法我可以用Javascript获取原始的CSS集字体大小的值吗?如何跨浏览器友好是您的方法?

Is there some way I can get the original CSS set font-size value with Javascript? How cross-browser friendly is your method?

提前感谢!

编辑: strong>我应该注意到,所有测试的浏览器(见下面的注释)允许我使用上面提到的两种方法使用'em'值设置文本大小,此时jQuery .css()返回等效的' px'值和Javascript .style.fontSize方法返回正确的'em'值。也许最好的方法是在页面加载时初始化元素,给它们一个em值。

I should note that all the tested browsers (See comment below) allow me to set the the text-size using an 'em' value using the two methods mentioned above, at which point the jQuery .css() returns an equivalent 'px' value and the Javascript .style.fontSize method returns the correct 'em' value. Perhaps the best way to do this would be to initialize the elements on page load, giving them an em value right away.

推荐答案

这个jQuery插件看起来就像这样做:

This jQuery plugin looks like it will do the trick:

Update: jQuery Plugin for Retaining Scalable Interfaces with Pixel-to-Em Conversion

Github:jQuery-Pixel-Em-Converter

这篇关于如何获取由css设置的元素的字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆