利用js动态创建 <style>

实例:

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



参考:http://webchina110.cn/?p=328

posted @ 2013-12-30 14:01  hdchangchang  阅读(176)  评论(0编辑  收藏  举报