js在head里插入style样式


代码如下:

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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



猜您在找 用javascript插入