当前位置:   article > 正文

HTML中将px转换为em的语法,在JavaScript中转换EM为PX(并获得默认字体大小)

js em 换算px

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中文网

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号