我将我的 css 上传到 github,然后转到站点上的文件并单击原始选项。我尝试将它添加到网页,但 chrome 给我以下错误:
Resource interpreted as Stylesheet but transferred with MIME type text/plain: "https://raw.githubusercontent.com/me/my-repo/master/style.css".
和
Cross-Origin Read Blocking (CORB) blocked cross-origin response https://raw.githubusercontent.com/me/my-repo/master/style.css with MIME type text/plain. See https://www.chromestatus.com/feature/5629709824032768 for more details.
如何才能成功添加此 CSS?我也在用 javascript 添加它:
var link = document.createElement('link');
link.setAttribute('rel', 'stylesheet');
link.setAttribute('type', 'text/css');
link.setAttribute('href', 'https://raw.githubusercontent.com/me/my-repo/master/style.css');
document.getElementsByTagName('head')[0].appendChild(link);
最佳答案
您可以在 Github Pages 上托管您的文件,只需转到 repo 设置[1],找到“Github Pages”部分并设置您的分支[2],然后单击“保存”。您将看到信息[3]。然后你去https://YOUR-GITHUB-USERNAME/REPO-NAME (如果您有 index.html 或任何文件,例如 /src/css/style.css
)您可以在任何站点上加载 CSS、JS 或其他文件
<link rel="stylesheet" href="path/to/file/style.min.css">
[1]:
[2]:
[3]:
关于javascript - 如何使用 github 托管外部 CSS 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63620956/