码迷,mamicode.com
首页 > Web开发 > 详细

JS动态生成<style>

时间:2016-12-13 08:12:17      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:生成   nod   back   document   ora   nts   line   one   node   

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

  

JS动态生成<style>

标签:生成   nod   back   document   ora   nts   line   one   node   

原文地址:http://www.cnblogs.com/gxsyj/p/6168425.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!