Home > Web Front-end > JS Tutorial > 使用Mootools动态添加Css样式表代码,兼容各浏览器_Mootools

使用Mootools动态添加Css样式表代码,兼容各浏览器_Mootools

WBOY
Release: 2016-05-16 17:58:22
Original
1137 people have browsed it

但是这样做就不利于插件的移植,所以就写了这个函数用来动态产生Css.

复制代码 代码如下:

AddCss=function(c){
var Sty=document.createElement('style');
Sty.type='text/css';
if(Browser.ie){
Sty.styleSheet.cssText=c;
}else{
var Frag=document.createDocumentFragment();
Frag.appendChild(document.createTextNode(c));
Sty.appendChild(Frag);
}
document.getElementsByTagName('head')[0].appendChild(Sty);
};
AddCss('a{background-color:red;color:black;padding:20px;}');

代码如上所示,比较简单就是一个Function而已,需要的时候就直接用AddCss添加代码即可.
注:该函数我在IE6;7;8;9以及Firefox内均已测试过,可以放心使用.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template