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

js动态添加style样式

时间:2018-07-08 16:49:52      阅读:646      评论:0      收藏:0      [点我收藏+]

标签:element   tno   color   type   ack   针对   nod   head   nts   

var style = document.createElement("style");

style.type = "text/css";

try{

  style.appendChild(document.createTextNode("body{background-color:red}"));

}catch(ex){

  style.styleSheet.cssText = "body{background-color:red}";//针对IE

}

var head = document.getElementsByTagName("head")[0];

head.appendChild(style);

 

js动态添加style样式

标签:element   tno   color   type   ack   针对   nod   head   nts   

原文地址:https://www.cnblogs.com/msg001/p/9280165.html

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