本站首页    管理页面    写新日志    退出


«August 2025»
12
3456789
10111213141516
17181920212223
24252627282930
31


公告
 本博客在此声明所有文章均为转摘,只做资料收集使用。

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:1304
评论数量:2242
留言数量:5
访问次数:7593895
建立时间:2006年5月29日




[Javascript]JavaScript动态加载CSS的三种方法
软件技术

lhwork 发表于 2006/12/29 9:18:14

如果你有很多关联的CSS文件要一起加载,或者想动态的加载不同的CSS文件,那么下面的方法你一定对你有帮助。第一种:一般用在外部CSS文件中加载必须的文件500)this.width=500'> 程序代码@import url(style.css);/*只能用在CSS文件中或者style标签中*/第二种:简单的在页面中加载一个外部CSS文件500)this.width=500'> 程序代码document.createStyleSheet(cssFile);第三种:用createElement方法创建CSS的Link标签500)this.width=500'> 程序代码var head = document.getElementsByTagName('HEAD').item(0);var style = document.createElement('link');style.href = 'style.css';style.rel = 'stylesheet';style.type = 'text/css';head.appendChild(style);这里贴上我以前在项目中使用的几个函数,希望对大家有用!500)this.width=500'> 程序代码function loadJs(file){    var scriptTag = document.getElementById('loadScript');    var head = document.getElementsByTagName('head').item(0);    if(scriptTag) head.removeChild(scriptTag);    script = document.createElement('script');    script.src = "../js/mi_"+file+".js";    script.type = 'text/javascript';    script.id = 'loadScript';    head.appendChild(script);}function loadCss(file){    var cssTag = document.getElementById('loadCss');    var head = document.getElementsByTagName('head').item(0);    if(cssTag) head.removeChild(cssTag);    css = document.createElement('link');    css.href = "../css/mi_"+file+".css";    css.rel = 'stylesheet';    css.type = 'text/css';    css.id = 'loadCss';    head.appendChild(css);}


阅读全文(3729) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.281 second(s), page refreshed 144770600 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号