在 IE 8 及以下版本中动态添加 @font-face 规则
我使用 IE stylesheet.addRule() 方法添加 @font-face 规则。但是,@ 符号对于该方法的“选择器”参数来说是不允许的字符,因此我收到“无效参数”错误。
s.addrule("@font-face", "font-family: 'Font Name'; src:url('/fonts/font.eot') etc...)";
还有其他方法可以动态添加这些规则吗?
我尝试设置 style 元素的 innerHTML 属性,设置 styleSheet 属性的 cssText 属性,并将文本节点附加到 style 元素(这会导致 IE 崩溃)。
还有其他方法可以尝试吗?
I'm adding @font-face rules using IEs stylesheet.addRule() method. However, the @ symbol is a disallowed character for the 'selector' argument of that method so I'm getting a 'invalid argument' error.
s.addrule("@font-face", "font-family: 'Font Name'; src:url('/fonts/font.eot') etc...)";
Is there some other way to dynamically add these rules?
I've tried setting the innerHTML property of the style element, setting the cssText property of the styleSheet property, and appending a text node to the style element as well (which crashes IE).
Any other methods to try?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
设置 cssText 属性确实有效,但您必须在将 @font-face 规则添加到文档之前将样式元素插入到文档中。例如...
据我所知,在将样式元素插入页面之前完全可以设置其他类型的CSS规则,但@font-face不行。
例如...这工作正常:
但这会使 IE 8 及以下版本崩溃:
Setting the cssText property does work, but you MUST insert the style element into the document before adding the @font-face rule to the document. Eg...
As far as I can tell it is perfectly possible to set other types of CSS rules before inserting the style element into the page, but not @font-face.
Eg... this works fine:
But this crashes IE 8 and less: