javascript - 如何在窗口中使用 Google 字体。在 React 中打开
问题描述
我有一个 React 应用程序,并在 onClick 处理程序中打开一个窗口。然后我通过以下方法将文本和样式添加到此窗口:
var myWindow = window.open("", "MsgWindow", "width=200,height=100");
myWindow.document.write("<html><head><style>CUSTOM CSS STYLES HERE</stye></head><p>This is 'MsgWindow'. I am 200px wide and 100px tall!</p></html>")
虽然这适用于传统 CSS,但如果我想添加从 Google Fonts 下载的自定义字体类型,则窗口将无法识别该字体。同样,向头部添加样式表链接似乎不起作用,因为窗口没有对 React 样式表的引用。
在这种情况下,如何使用通常在 window.open 中不可用的 Google?
我知道有一个用于打开窗口的 React 包,但我想避免这种情况。
解决方案
这可以解决问题
var myWindow = window.open("", "MsgWindow", "width=200,height=100");
myWindow.document.write("<html><head><link href='https://fonts.googleapis.com/css?family=Roboto' rel='stylesheet'> <style>body {font-family: 'Roboto', sans-serif;}</stye></head><p>This is 'MsgWindow'. I am 200px wide and 100px tall!</p></html>")
推荐阅读
- kotlin - Kotlin:当谓词满足时停止迭代的函数
- laravel-migrations - 在 laravel 迁移中使用 UUID 作为默认值
- javascript - Highcharts 将变量传递给工具提示
- javascript - 如何在javascript中重新排列数组
- c++ - 连接池析构函数阻止子对象析构函数中的网络通信
- go - redigo 是否重新连接到服务器?
- windows - Windows 操作系统的电源选项或注册表中的屏幕唤醒设置?
- highcharts - 当大量 x 轴刻度时,Highcharts 仅显示特定刻度
- ads - 在网站上轮换 CPM 广告的简单算法
- powershell - 导出 Csv 帮助 PowerShell