【问题标题】:how to use css codes in javascript file如何在javascript文件中使用css代码
【发布时间】:2019-01-01 02:16:06
【问题描述】:

我有一个JS代码:

fontCode += "*{font-family: " + sendFont.f + ", fontAwesome !important;}";

我想通过css添加一些字体font-face 所以我将font-face 代码添加到一个名为font-face.css 的css 文件中 然后我尝试将font-face.css 文件包含到 JS 中,我将 js 代码更改为:

fontCode += "@import url('font-face.css'); *{font-family: " + sendFont.f + ", fontAwesome !important;}";

但它没有用!我不知道如何在我的 js 文件中使用 css font-face

【问题讨论】:

标签: javascript html css font-face


【解决方案1】:

WebFont.load({
    google: {
        families: ['Droid Sans', 'Droid Serif']
    }
});
How to import fonts with pure JS?

var newStyle = document.createElement('style');
newStyle.appendChild(document.createTextNode("\
@font-face {\
    font-family: " + yourFontName + ";\
    src: url('" + yourFontURL + "') format('yourFontFormat');\
}\
"));

document.head.appendChild(newStyle);

提问前请先搜索

Font-Face changing via JavaScript

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2013-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-09
  • 1970-01-01
  • 1970-01-01
  • 2022-06-17
相关资源
最近更新 更多