javascript - 如何在javascript文件中使用css代码

标签 javascript html css font-face

我有一个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

最佳答案

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

关于javascript - 如何在javascript文件中使用css代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51498933/

相关文章:

javascript - 在另一个 ViewComponent 中调用一个 ViewComponent

javascript - Redux-Saga 无法正确读取来自 fetch api 的响应

javascript - Ng Bootstrap 轮播全屏

javascript - Chrome 扩展程序 : Is there a means of running JavaScript to procure inner html on a page and saving it to a variable in the extension?

javascript - SweetAlert - 更改模式颜色

javascript - 通过纯 CSS 在超时后渲染一个组件

javascript - 澄清使用javascript检测ie

javascript - 我怎么知道我在哪一类中徘徊,javascript

php - 表单到 iframe 提交 - 文件上传后如何重置表单?

HTML/CSS : how to remove the box around link element?