getComputedStyle在自定义Web组件中返回font-size:0px

时间:2015-02-27 18:21:09

标签: javascript css fonts custom-component shadow-dom

我试图从vanira javaScript自定义组件中的调用文档中获取字体大小。

我打电话的时候:

var div = shadowRoot.querySelector('div');
console.log(window.getComputedStyle(div));

我得到了一个CSSStyleDeclaration,其中包含了您所期望的所有内容:

font: "normal normal normal normal 24px/normal Times"
fontFamily: "Times"
fontKerning: "auto"
fontSize: "24px"
fontStretch: "normal"
fontStyle: "normal"
fontVariant: "normal"
fontVariantLigatures: "normal"
fontWeight: "normal"

但是当我在下一行拨打以下任何一个时:

console.log(window.getComputedStyle(div).getPropertyValue('font-size'));
console.log(window.getComputedStyle(div).fontSize);
console.log(window.getComputedStyle(div)['font-size']);
console.log(window.getComputedStyle(div)['fontSize']);

返回' 0px'。

即使我使用' font'调用相同的方法,它也会返回正常的正常正常0px /正常'。

知道为什么或如何重置CSSStyleDeclaration以及如何解决它。或者是否有更简单的方法在shadowDom模板中获取调用doc的字体上下文?

0 个答案:

没有答案
相关问题