代码如下:
1 var nod = document.createElement('style'), 2 str = 'body{background:#000;color:#fff} a{color:#fff;text-decoration:none;} a:hover{color:red;text-decoration:underline}'; 3 nod.type='text/css'; 4 if (nod.styleSheet) { //ie下 5 nod.styleSheet.cssText = str; 6 } else { 7 nod.innerHTML = str; //或者写成 nod.appendChild(document.createTextNode(str)) 8 } 9 document.getElementsByTagName('head')[0].appendChild(nod);