使用jquery动态加载js文件的方法

时间:2022-09-15 00:00:54

方法很简单,这里就不多废话了,直接奉上代码:

 

复制代码 代码如下:

$.extend({
     includePath: '',
     include: function(file) {
        var files = typeof file == "string" ? [file]:file;
        for (var i = 0; i < files.length; i++) {
            var name = files[i].replace(/^\s|\s$/g, "");
            var att = name.split('.');
            var ext = att[att.length - 1].toLowerCase();
            var isCSS = ext == "css";
            var tag = isCSS ? "link" : "script";
            var attr = isCSS ? " type='text/css' rel='stylesheet' " : " language='javascript' type='text/javascript' ";
            var link = (isCSS ? "href" : "src") + "='" + $.includePath + name + "'";
            if ($(tag + "[" + link + "]").length == 0) $("body").append("<" + tag + attr + link + "></" + tag + ">");
        }
   }
});

 

//使用方法
$.includePath = '../js/Exam';
$.include(['/AddQuestion.js']);

 

小伙伴们如果有什么疑问就在下面留言吧,大家共同进步。