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

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

    Straight-up jQuery way (that works on major browsers - including Chrome): http://topsecretproject.finitestatemachine.com/2009/09/how-to-load-javascript-and-css-dynamically-with-jquery/

    From the site:

    $("head").append("");
    var CSS = $("head").children(":last");
    CSS.attr({
    "rel": "stylesheet",
    "type": "text/css",
    "href": "url/to.css"
    });
    

    Note: the example also includes injection of JS, which can be ignored if you just want to inject a CSS reference.

提交回复
热议问题