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

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

问题描述

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



有一些方法我可以得到原始的CSS设置font-size值与Javascript?



编辑:

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

解决方案

这个jQuery插件看起来像它会做的诀窍:



http://www.filamentgroup.com/lab/update_jquery_plugin_for_retaining_scalable_interfaces_with_pixel_to_em_con/


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.

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

Thanks in advance!

Edit: 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.

解决方案

This jQuery plugin looks like it will do the trick:

http://www.filamentgroup.com/lab/update_jquery_plugin_for_retaining_scalable_interfaces_with_pixel_to_em_con/

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

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