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

后端 未结 8 1877
悲哀的现实
悲哀的现实 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:22

    Here's the gig. What Patrick said did not work for me. Here's how I did it.

        var ref = document.createElement('style');
    ref.setAttribute("rel", "stylesheet");
    ref.setAttribute("type", "text/css");
    document.getElementsByTagName("head")[0].appendChild(ref);
    if(!!(window.attachEvent && !window.opera)) ref.styleSheet.cssText = asd;//this one's for ie
    else ref.appendChild(document.createTextNode(asd));
    

    InnerHTML is deprecated and shouldn't be used for such a thing. Furthermore it is slower too.

提交回复
热议问题