使用jquery动态加载js文件的方法_jquery
程序员文章站
2022-04-24 20:17:49
...
方法很简单,这里就不多废话了,直接奉上代码:
$.extend({
includePath: '',
include: function(file) {
var files = typeof file == "string" ? [file]:file;
for (var i = 0; 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 + ">");
}
}
});
复制代码 代码如下:
$.extend({
includePath: '',
include: function(file) {
var files = typeof file == "string" ? [file]:file;
for (var i = 0; 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 + ">");
}
}
});
//使用方法
$.includePath = '../js/Exam';
$.include(['/AddQuestion.js']);
小伙伴们如果有什么疑问就在下面留言吧,大家共同进步。
上一篇: jquery其他插件之comet(六)
下一篇: 使用HTML应该如何编写邮件模版