Jquery无法在回调函数中访问$(this)

发布于 2024-09-11 11:13:25 字数 618 浏览 2 评论 0原文

我正在创建一个插件,但它无法访问 $(this)。我的插件的一个简单概述是

(function($){
    $.fn.myPlugin= function(options, callback) {
        return this.each(function(){
                $(this).click(function(){
                      // some plugin works ..

                      callback();
                });
        });
    };

})(jQuery);

然后我将我的插件附加到像这里这样的元素,

$('p').myPlugin({
      // Some options
}, function(){
      alert('first test');
      alert($(this).text());
});

当单击元素 p 时,我收到第一个警报,但我没有收到第二个警报。

回调函数被调用,但无法访问this。 定义或代码有问题吗?任何替代建议也会有帮助

I am creating a plugin and it cannot access $(this). A simple overview of my plugin is

(function($){
    $.fn.myPlugin= function(options, callback) {
        return this.each(function(){
                $(this).click(function(){
                      // some plugin works ..

                      callback();
                });
        });
    };

})(jQuery);

Then i attached my plugin to a element like

$('p').myPlugin({
      // Some options
}, function(){
      alert('first test');
      alert($(this).text());
});

Here when the element p is clicked, i get the first alert, but i didn't get the 2nd alert.

The callback function is called, but unable to access this.
Is there any problem with the defining or with code ? any alternative suggestion will also be helpful

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

你是我的挚爱i 2024-09-18 11:13:26

使用 < 而不是 callback(); code>.apply() 为其提供正确的上下文(否则它是 window),如下所示:

callback.apply(this);

您可以在此处查看更新/工作版本


为了在这里获得更完整的概述,如果您想要该回调,您可以传递更多参数,例如,如果您想让选项可用,您可以这样做:

(function($){
  $.fn.myPlugin= function(options, callback) {
    return this.each(function(){
      $(this).click(function(){
        callback.apply(this, [options]);
     });
   });
 };
})(jQuery);

然后像这样调用它:

$('p').myPlugin({
    thing: "thing1"
}, function(opts){
    alert(opts.thing); //thing1
});​

您可以在这里尝试一下,只需添加您想要的任何参数即可想要该数组,将使用这些参数调用 callback() :)

Instead of callback();, use .apply() to give it the right context (otherwise it's window), like this:

callback.apply(this);

You can see the updated/working version here.


For a more complete overview here, you can pass more arguments if you want to that callback, for example if you wanted to make the options available, you could do this:

(function($){
  $.fn.myPlugin= function(options, callback) {
    return this.each(function(){
      $(this).click(function(){
        callback.apply(this, [options]);
     });
   });
 };
})(jQuery);

Then call it like this:

$('p').myPlugin({
    thing: "thing1"
}, function(opts){
    alert(opts.thing); //thing1
});​

You can give that one a try here, just add whatever arguments you want to that array, callback() will be called with those arguments :)

分開簡單 2024-09-18 11:13:26

尼克·克拉弗是对的。但要了解发生了什么,请点击以下链接:
http://howtonode.org/what-is-this

Nick Craver is right. But for understanding what is going on follow this link:
http://howtonode.org/what-is-this

苦妄 2024-09-18 11:13:26

您能否发布一个使用自动完成小部件的完整示例?

这就是我想做的...

    $('.suggest').autocomplete({
    source: function(request, response) {
        typ = $(this).getAttr("class").split(" ")[2]
        ....

...但是 this 返回未定义。目前我正在使用 document.activeElement,但我更喜欢 Nick Craver 的方法。

Can you please post a complete example that uses the autocomplete widget?

Here is what I wanted to do...

    $('.suggest').autocomplete({
    source: function(request, response) {
        typ = $(this).getAttr("class").split(" ")[2]
        ....

...but this returns undefined. For now I am using document.activeElement, but I would prefer Nick Craver's method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文