jquery 将变量传递给函数

发布于 2024-08-22 09:48:06 字数 1462 浏览 6 评论 0原文

大家好,我正在制作一个简单的预加载函数

(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

≈。彩虹 2024-08-29 09:48:06

如果我理解正确,您想将迭代索引传递给 .after()。

在您的插件中,变量 counter 用于跟踪您所处的each() 迭代的哪一步。这是不必要的,因为each 的第一个参数是循环索引。请参阅文档中的 each

此外,不需要函数 is_last_done(),因为 limit 变量在each() 调用的匿名函数的范围内。

var limit = this.length - 1;

settings.before();

this.each(function(i, src){
    $("<img>").attr("src", src).load(function(){

        if (i == a) settings.after(i);
        if (i == limit) settings.end();

    });
});

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().

var limit = this.length - 1;

settings.before();

this.each(function(i, src){
    $("<img>").attr("src", src).load(function(){

        if (i == a) settings.after(i);
        if (i == limit) settings.end();

    });
});

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.

初心 2024-08-29 09:48:06

该变量可以在嵌套函数上访问,因为它位于外部 闭包 上,您不需要参数,你可以简单地使用它:

var img = ['a.jpg', 'b.jpg', 'b.jpg'];
var b = 1;

$(img).preload({
    before: function(){ return; },
    end: function(){  },
    after: function(){
        // b is accessible here.
        console.log(b);
    }
});

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:

var img = ['a.jpg', 'b.jpg', 'b.jpg'];
var b = 1;

$(img).preload({
    before: function(){ return; },
    end: function(){  },
    after: function(){
        // b is accessible here.
        console.log(b);
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文