更新时间:2023-12-04 22:20:28
CSS不是脚本,因此您不必从脚本执行的意义上执行"它.
CSS is not a script, so you dont have to "execute" it in the sense of script execution.
基本上,动态创建的<link>
标签并附加到标头应该足够,例如
Basically a <link>
tag created on the fly and appended to the header should suffice, like
$('<link/>', {
rel: 'stylesheet',
type: 'text/css',
href: 'path_to_the.css'
}).appendTo('head');
或
var linkElem = document.createElement('link');
document.getElementsByTagName('head')[0].appendChild(linkElem);
linkElem.rel = 'stylesheet';
linkElem.type = 'text/css';
linkElem.href = 'path_to_the.css';
如果要在不使用jQuery的情况下进行操作.
if you want to do it without jQuery.
浏览器将响应DOM中的更改并相应地更新页面布局.
The browser will respond to the change in the DOM and update your page layout accordingly.
编辑:
我已经阅读到旧的Internet Explorer遇到了麻烦,您可能需要像回答中那样使其工作:
I have read that old Internet Explorer has trouble with this, and you might need to do it like in answer to make it work:
https://***.com/a/2685639/618206
EDIT2 :
读取文件内容并将其内联(在<style>
标记之间)也是一种有效的解决方案,但是那样CSS不会被浏览器缓存.
Reading the file content and putting it inline (between <style>
tags) is also a valid solution, but that way the CSS will not be cached by the browser.