赞
踩
CommunityChannel72提出了一个问题:Converting em to px in Javascript (and getting default font size),或许与您遇到的问题类似。
Edit: No, there isn't.
To get the rendered font size of a given element, without affecting the DOM:
parseFloat(getComputedStyle(parentElement).fontSize);
This is based off the answer to this question.
Edit:
In IE, you would have to use parentElement.currentStyle["fontSize"], but this is not guaranteed to convert the size to px. So that's out.
Furthermore, this snippet won't get you the default font size of the element, but rather its actual font size, which is important if it has actually got a class and a style associated with it. In other words, if the element's font size is 2em, you'll get the number of pixels in 2 ems. Unless the font size is specified inline, you won't be able to get the conversion ratio right.
希望本文对你有帮助,欢迎支持JavaScript中文网
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。