据我所知,没有办法在动态 JS 文件中使用 {% include %} 来包含样式。但我不想再次调用服务器来下载样式。
也许可以通过获取样式表并将其注入(inject)到文档的 head 元素中......以前有人这样做过吗?
最佳答案
在你的 JS 文件中:
var style = document.createElement('link');
style.setAttribute('rel', 'stylesheet');
style.setAttribute('type', 'text/css');
style.setAttribute('href', 'style.css');
document.getElementsByTagName('head')[0].appendChild(style);
希望有帮助。
关于javascript - 是否可以使用 Django 从 JS 文件中包含 CSS(这是棘手的部分)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/379827/