纯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;}');

 

相关文章:

  • 2022-12-23
  • 2021-12-18
  • 2022-02-17
  • 2022-01-24
  • 2022-12-23
  • 2022-12-23
  • 2021-12-22
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-02-19
  • 2022-12-23
  • 2022-12-23
  • 2021-12-16
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案