利用js动态创建 style

发布时间:2017年2月8日 作者:未知 查看次数:1254

自:http://blog.csdn.net/hdchangchang/article/details/17680063

代码:


var nod = document.createElement("style"),   

str = "body{background:#000;color:#fff} a{color:#fff;text-decoration:none;} a:hover{color:red;text-decoration:underline}";  

nod.type="text/css";  

if(nod.styleSheet){         //ie下  

nod.styleSheet.cssText = str;  

} else {  

nod.innerHTML = str;       //或者写成 nod.appendChild(document.createTextNode(str))  

}  

document.getElementsByTagName("head")[0].appendChild(nod);




版权所有!www.sieye.cn
E.Mail:sieye@sohu.com QQ:66697110