1、创建link标签加载CSS文件
javascript
function loadCSS(cssFile) {
var link = document.createElement("link");
link.rel = "stylesheet";
link.type = "text/css";
link.href = cssFile;
document.head.appendChild(link);
}
// 异步加载CSS文件
loadCSS('path/to/your.css');
2、动态创建<style>标签,将CSS代码插入到文档中
javascript
function loadCSSCode(cssCode) {
var style = document.createElement('style');
style.type = 'text/css';
if (style.styleSheet) {
// This is required for IE8 and below.
style.styleSheet.cssText = cssCode;
} else {
style.appendChild(document.createTextNode(cssCode));
}
document.getElementsByTagName("head")[0].appendChild(style);
}
// 动态加载CSS代码
loadCSSCode('.classname { color: red; }');
3、利用import(vue中)
import('./path/to/your.css').then(css => css.default);