Element.appendChild() chokes in IE

前端 未结 4 1704
予麋鹿
予麋鹿 2020-12-03 19:36

I have the following javascript:

  css = document.createElement(\'style\');
  css.setAttribute(\'type\', \'text/css\');
  css_data = document.createTextNode(         


        
4条回答
  •  眼角桃花
    2020-12-03 20:18

    Try instead:

    var css = document.createElement('style');
    css.setAttribute('type', 'text/css');
    
    var cssText = '';
    if(css.styleSheet) { // IE does it this way
        css.styleSheet.cssText = cssText
    } else { // everyone else does it this way
        css.appendChild(document.createTextNode(cssText));
    }
    
    document.getElementsByTagName("head")[0].appendChild(css);
    

提交回复
热议问题