jquery 将变量传递给函数
大家好,我正在制作一个简单的预加载函数
(function($) {
$.fn.preload = function(settings) {
var config = {
before: function(){ return; },
end: function(){ return; },
after: function(a){ return; }
};
if (settings) $.extend(config, settings);
var limit = this.length - 1;
var counter = 0;
settings.before();
is_last_done = function(i){if (i == limit) return true;};
this.each(function(i, src){
$("<img>").attr("src", src).load(function(){
if( a == counter ) settings.after();
if(is_last_done(i)) settings.end(); // Last is done
counter++;
});
});
return this;
};
})(jQuery);
,并调用它,但
img = ['a.jpg', 'b.jpg', 'b.jpg'];
a = 1;
$(img).preload({
before: function(){ return; },
end: function(){ },
after: function(a){
console.log('first done');
}
});
问题是传递给“after()”函数的“a”变量没有被传递。
在插件内部,我可以使用“a”访问变量,就像在这一行中
if( a == counter ) settings.after();
“a”可用,但是如果我想将 a 变量命名为其他名称怎么办?如何访问 after() 函数的参数?
我的代码将不再工作
如果我使用此代码img = ['a.jpg', 'b.jpg', 'b.jpg'];
b = 1;
$(img).preload({
before: function(){ return; },
end: function(){ },
after: function(b){
console.log('first done');
}
});
b 没有通过,有什么想法吗?
谢谢
Hay guys, I'm making a simple preload function
(function($) {
$.fn.preload = function(settings) {
var config = {
before: function(){ return; },
end: function(){ return; },
after: function(a){ return; }
};
if (settings) $.extend(config, settings);
var limit = this.length - 1;
var counter = 0;
settings.before();
is_last_done = function(i){if (i == limit) return true;};
this.each(function(i, src){
$("<img>").attr("src", src).load(function(){
if( a == counter ) settings.after();
if(is_last_done(i)) settings.end(); // Last is done
counter++;
});
});
return this;
};
})(jQuery);
and calling it with
img = ['a.jpg', 'b.jpg', 'b.jpg'];
a = 1;
$(img).preload({
before: function(){ return; },
end: function(){ },
after: function(a){
console.log('first done');
}
});
the problem is that the 'a' variable im passing to the 'after()' function is not being passed.
Inside the plugin i can access the variable with 'a', like on this line
if( a == counter ) settings.after();
The 'a' is availabe, however what if i want to name the a variable to something else? How do i access the argument of the after() function?
My code won't work anymore if i use this code
img = ['a.jpg', 'b.jpg', 'b.jpg'];
b = 1;
$(img).preload({
before: function(){ return; },
end: function(){ },
after: function(b){
console.log('first done');
}
});
b doesnt get passed, any ideas?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解正确,您想将迭代索引传递给 .after()。
在您的插件中,变量 counter 用于跟踪您所处的each() 迭代的哪一步。这是不必要的,因为each 的第一个参数是循环索引。请参阅文档中的 each。
此外,不需要函数 is_last_done(),因为 limit 变量在each() 调用的匿名函数的范围内。
JavaScript 具有词法作用域,因此您无需为函数执行任何操作即可查看封闭作用域中声明的变量。这被正确地称为闭包,并在这篇 Mozilla 文章中进行了描述 - 使用闭包。
If I understand correctly, you want to pass the iteration index to .after().
In your plugin, variable counter is used to track which step of the each() iteration you are in. This is unnecessary, as the first parameter to each is the loop index. See each in the documentation.
Also, function is_last_done() is not needed as the limit variable is in scope for the anonymous function called by each().
Javascript is lexically scoped, so you do not have to do anything for a function to see variables declared in enclosing scopes. This is properly termed closure, and is described in this Mozilla article - Working with Closures.
该变量可以在嵌套函数上访问,因为它位于外部 闭包 上,您不需要参数,你可以简单地使用它:
The variable is accessible on your nested functions, because it lives on the outer closure, you don't need the argument, you can simply use it: