如何使用 JavaScript 导入字体文件? [英] How to import font file using JavaScript?

查看:63
本文介绍了如何使用 JavaScript 导入字体文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CSS 有一个 @font-face 允许我们使用"自定义字体的规则.

CSS has a @font-face rule which allows us to "use" custom fonts.

JavaScript 有这个功能吗?

Does JavaScript have this functionality?

更具体地说,是否可以在不使用 CSS @font-face 规则的情况下使用"自定义字体?

More specifically, is it possible to "use" custom fonts without using the CSS @font-face rule?

推荐答案

除了使用 @font-face 规则之外,没有办法让浏览器从网络加载字体.您可以理论上从 JavaScript 创建该 @font-face 规则,而无需使用 document.write(或 document.createElement("style") 等),如果您使用的是 CSS 对象模型;但我目前不指望它会在任何浏览器中实现.

There is no way to cause the browser to load a font from the network other than with a @font-face rule. You could theoretically create that @font-face rule from JavaScript without having to use document.write (or document.createElement("style") etc), if instead you used the CSS Object Model; but I wouldn't count on that to be implemented in any browser at the moment.

这篇关于如何使用 JavaScript 导入字体文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆