简体   繁体   English

如何获取由css设置的元素的字体大小

[英]How can I get the font size of an element as it was set by css

I'm writing a simple jQuery that to change the font size of an element by a certain percentage. 我正在编写一个简单的jQuery,用于将元素的字体大小更改一定百分比。 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. 我遇到的问题是,当我使用jQuery的$('#el')。css('font-size')获得大小时,它总是返回一个像素值,即使用em设置也是如此。 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. 当我使用Javscript的el.style.font-size属性时,它将不会返回一个值,直到一个值被同一属性显式设置。

Is there some way I can get the original CSS set font-size value with Javascript? 有什么方法可以用Javascript获得原始CSS设置的字体大小值吗? 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. 编辑:我应该注意所有经过测试的浏览器(请参阅下面的评论)允许我使用上面提到的两种方法使用'em'值设置文本大小,此时jQuery .css()返回一个等效的' px'的值和Javascript .style.fontSize方法返回正确的'em'值。 Perhaps the best way to do this would be to initialize the elements on page load, giving them an em value right away. 也许最好的方法是在页面加载时初始化元素,立即为它们提供em值。

All of your target browsers with the exception of IE will report to you the "Computed Style" of the element. 除IE之外的所有目标浏览器都会向您报告元素的“计算样式” In your case you don't want to know what the computed px size is for font-size , but rather you want the value set in your stylesheet(s). 在您的情况下,您不想知道计算的px大小对于font-size是什么,而是您想要在样式表中设置的值。

Only IE can get this right with its currentStyle feature. 只有IE才能通过其currentStyle功能实现这一目标。 Unfortunately jQuery in this case works against you and even gets IE to report the computed size in px (it uses this hack by Dean Edwards to do so, you can check the source yourself). 不幸的是,jQuery在这种情况下你起作用,甚至让IE报告px的计算大小(它使用Dean Edwards的这个黑客来做,你可以自己查看源代码 )。

So in a nutshell, what you want is impossible cross-browser. 简而言之,你想要的是跨浏览器是不可能的。 Only IE can do it (provided you bypass jQuery to access the property). 只有IE可以做到(假设您绕过jQuery访问该属性)。 Your solution of setting the value inline (as opposed to in a stylesheet) is the best you can do, as then the browser does not need to compute anything. 您设置内联值(而不是样式表)的解决方案是您可以做的最好的解决方案,因为浏览器不需要计算任何内容。

i had this problem once. 我曾经遇到过这个问题。 i use this function to get the int value of a css attribute, if there is one. 我使用此函数来获取css属性的int值(如果有)。

function PBMtoInt(str)
{ 
return parseInt(str.replace(/([^0-9\.\-])+/,"")!=""?str.replace(/([^0-9\.\-])+/,""):"0");
}

In Chrome: 在Chrome中:

var rules = window.getMatchedCSSRules(document.getElementById('target'))

returns a CSSRuleList object. 返回一个CSSRuleList对象。 Need to do a bit more experimentation with this, but it looks like if m < n then the CSSStyleRule at rules[n] overrides the one at rules[m] . 需要对此进行更多的实验,但看起来如果m < n则则rules[n]CSSStyleRule会覆盖rules[m]CSSStyleRule So: 所以:

for(var i = rules.length - 1; i >= 0; --i) {
    if(rules[i].style.fontSize) {
        return /.*(em|ex|ch|rem|vh|vw|vmin|vmax|px|in|mm|cm|pt|pc|%)$/.exec(rules[i].style.fontSize)[1];
    }
}

In Firefox, maybe use sheets = document.styleSheets to get all your stylesheets as a StyleSheetList , then iterate over each CSSStyleSheet sheet in sheets . 在Firefox中,可以使用sheets = document.styleSheets将所有样式表作为StyleSheetList ,然后在工作CSSStyleSheet sheet中迭代每个CSSStyleSheet sheet sheets Iterate through the CSSStyleRule s in sheet (ignoring the CSSImportRule s) and test each rule against the target element via document.getElementById('target').querySelector(rule.selectorText) . 通过迭代CSSStyleRule S IN sheet (忽略CSSImportRule或多个),并测试每个规则针对经由所述目标元件document.getElementById('target').querySelector(rule.selectorText) Then apply the same regexp as above..... but that's all just a guess, I haven't tested it out or anything.... 然后应用与上面相同的正则表达式.....但这只是一个猜测,我没有测试过它或任何东西....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM