How do I load css rules dynamically in Webkit (Safari/Chrome)?

后端 未结 8 1859
悲哀的现实
悲哀的现实 2020-12-10 17:16

I currently have issues in Webkit(Safari and Chrome) were I try to load dynamically (innerHTML) some html into a div, the html contains css rules (...), after the html gets

相关标签:
8条回答
  • 2020-12-10 17:36

    meh. I don't have enough points to vote up Andrei Cimpoca's result, but that solution is the best one here.

    style.innerHTML = "..."; does not work in webkit engines or ie.

    To correctly inject css text, you must:

    style.styleSheet.cssText = "..."; for ie.

    and

    style.appendChild(document.createTextNode("...")); for webkit.

    Firefox will also work with the second option, or with style.innerHTML = "...";

    0 讨论(0)
  • 2020-12-10 17:42

    I know it's an older topic, but thought someone would benefit. I too needed to load css dynamically into my page (before everything rendered onload, according to included "component" templates):

    var oStyle = $("<style />");
    
    oStyle.append("@import url(" + sTemplateCssUrl + ");");
    oStyle.attr("type", "text/css");
    
    $(document.head).append(oStyle);
    

    I tried appending to document.head with a one-line approach -- worked in IE 10 and Chrome, but not in Safari. The approach above worked in all 3.

    0 讨论(0)
提交回复
热议问题