纯js实现:
var css = ‘h1 { background: red; }‘, head = document.getElementsByTagName(‘head‘)[0], style = document.createElement(‘style‘); style.type = ‘text/css‘; if(style.styleSheet){ style.styleSheet.cssText = css; }else{ style.appendChild(document.createTextNode(css)); } head.appendChild(style);
jquery实现:
var $style = $(‘<style type="text/css"></style>‘); $($(‘head‘)[0]).append($style); $style.append(‘.test{background: red;}‘);
原文:http://www.cnblogs.com/yghost/p/4592730.html