如何实现JavaScript动态加载CSS和JS文件

2024-11-24 16:24:16
推荐回答(1个)
回答1:

动态加载css文件:
function loadStyles(url) {
var link = document.createElement("link");
link.type = "text/css";
link.rel = "stylesheet";
link.href = url;
document.getElementsByTagName("head")[0].appendChild(link);
}

loadStyles("css/secondindex.css");

动态加载js文件:

function loadScript(url) {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = url;
document.body.appendChild(script);
}

loadScript("javascript/lib/cookie.js");