如何从jquery创建一个js函数,可以在多个脚本中使用? [英] How can I create a js function from a jquery one, that can be used in multiple scripts?
问题描述
我有一个名为utils.js的脚本,其中包含多个可以跨多个文件重复使用的函数。那么如何在调用时以多种文件的方式转换此函数?
示例:
function myMultiscriptsGetter(pScript){
// 执行$ .getMultiScripts的相同逻辑
// 注意:此函数将在我的utils.js文件中
}
然后在任何其他脚本中我只需要包含utils.js并调用函数,如下所示:
$。myMultiscriptsGetter(pScript)。完成( function (){
// 所有脚本都已加载!
});
注意:我试过这个但它不起作用。我没有很多jquery的经验
我尝试过:
目前,我有这个功能:
$。getMultiScripts = function (arr,path){
var _arr = $ .map(arr, function (scr){
return $ .getScript((path || )+ scr);
});
_arr.push($。延期( function (延期){
$(deferred.resolve);
}));
return $ .when.apply($,_arr);
}
然后,被称为:
< pre lang =Javascript> $。getMultiScripts(pScript).done( function (){
// 加载所有脚本!
});
.getMultiScripts
// 注意:此函数将在我的utils中。 jsfile
}
然后在任何其他脚本中我只需要包括utils.js并调用函数,如下所示:
.myMultiscriptsGetter(pScript).done( function (){
// 加载所有脚本!
});
注意:我试过这个,但它不起作用。我没有很多jquery的经验
我尝试过:
目前,我有这个功能:
.getMultiScripts = function (arr,path){
var _arr =
I have a script called "utils.js" that contains multiple functions that can be reused cross multiple files. So how can i convert this function in a way to work in multiple files when is called?
Example:
function myMultiscriptsGetter(pScript){
// do the same logic of $.getMultiScripts
//Note: this function will be in my "utils.js" file
}
Then in any other script i just need to include the "utils.js" and call the function, like this :
$.myMultiscriptsGetter(pScript).done(function() {
// all scripts loaded !
});
Note: I tried this but it doesn't work. I dont have a lot experience with jquery
What I have tried:
Currently, i have this function:
$.getMultiScripts = function(arr, path) {
var _arr = $.map(arr, function(scr) {
return $.getScript( (path||"") + scr );
});
_arr.push($.Deferred(function( deferred ){
$( deferred.resolve );
}));
return $.when.apply($, _arr);
}
Then, is called:
$.getMultiScripts(pScript).done(function() {
// all scripts loaded !
});
.getMultiScripts //Note: this function will be in my "utils.js" file }
Then in any other script i just need to include the "utils.js" and call the function, like this :
.myMultiscriptsGetter(pScript).done(function() { // all scripts loaded ! });
Note: I tried this but it doesn't work. I dont have a lot experience with jquery
What I have tried:
Currently, i have this function:
.getMultiScripts = function(arr, path) { var _arr =
这篇关于如何从jquery创建一个js函数,可以在多个脚本中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!