js动态添加style样式2
2018-07-08 16:12 忆- 阅读(2771) 评论(0) 收藏 举报function loadStyleString(css){
var style = document.createElement("style");
style.type = "text/css";
try{
style.appendChild(document.createTextNode(css));
}catch(ex){
style.styleSheet.cssText = css;//兼容IE
}
var head = document.getElementsByTagName("head")[0];
head.appendChild(style);
}
调用示例
loadStyleString("body{background-color:red}");
浙公网安备 33010602011771号