zoukankan      html  css  js  c++  java
  • JavaScript动态加载CSS和JS文件

    var dynamicLoading = {

      css: function(path){
     if(!path || path.length === 0){
      throw new Error('argument "path" is required !');
     }
     var head = document.getElementsByTagName('head')[0];
        var link = document.createElement('link');
        link.href = path;
        link.rel = 'stylesheet';
        link.type = 'text/css';
        head.appendChild(link);
      },
      js: function(path){
     if(!path || path.length === 0){
      throw new Error('argument "path" is required !');
     }
     var head = document.getElementsByTagName('head')[0];
        var script = document.createElement('script');
        script.src = path;
        script.type = 'text/javascript';
        head.appendChild(script);
      }
    }
    //动态加载 CSS 文件
    dynamicLoading.css("test.css");
     
    //动态加载 JS 文件
    dynamicLoading.js("test.js");
  • 相关阅读:
    既然选择了远方,就只顾风雨兼程!
    slots
    面向对象
    模块和作用域
    偏函数
    python中decorator
    返回函数
    filter, sort
    map/reduce
    开发步骤
  • 原文地址:https://www.cnblogs.com/liushunli/p/11377057.html
Copyright © 2011-2022 走看看