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);} |
|
|