且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

动态加载CSS样式表并等待其加载

更新时间:2023-12-04 23:51:40

您需要在头部插入一个link节点,然后将onload事件附加到该节点.在您的代码链接中是一个字符串.有关如何实现所需内容的信息,请参见下面的示例代码.

You need to insert a link node in the head and then attach onload event to that. In your code link is a String. See sample code below on how to implement what you want.

var link = document.createElement('link');
link.setAttribute("rel", "stylesheet");
link.setAttribute("type", "text/css");
link.onload = function(){ CSSDone(); }
link.setAttribute("href", 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css');
document.getElementsByTagName("head")[0].appendChild(link);

jsfiddle