js怎么引入css文件?
1、使用document.write方式输出引入css的link标签
在调用文件的顶部加入下例代码
document.write('');
2、使用createElement方法动态创建link标签,来引入css
在将下边代码放入Body中
new_element = document.createElement('link');
new_element.setAttribute('rel', 'stylesheet');
new_element.setAttribute('href', 'main.css');
document.body.appendChild(new_element);
我们来分析一下关键的几句代码:
首先我们利用document.createElement('link')生成了一个link标签;
然后设置其rel属性为stylesheet,href为main.css;
最后将这个标签动态地加入body中。
更多web前端知识,请查阅 HTML中文网 !!