用JavaScript来加载css、js文件

2023-10-21 13:54:16 浏览数 (3)

用javaScript加载css、js
代码语言:javascript复制

function addLink(url) {
  var link = document.createElement("link");
  link.rel = "stylesheet";
  link.type = "text/css";
  link.href = url;
  document.getElementsByTagName("head")[0].appendChild(link);
}


function addScript(url) {
  var newscript = document.createElement('script');
  newscript.setAttribute('type','text/javascript');
  newscript.setAttribute('src',url);
  var head = document.getElementsByTagName('head')[0];
  head.appendChild(newscript);
}

addScript("js文件链接");

addLink("css文件的链接");

0 人点赞