如何在Fancybox的onComplete事件中使用$(this)? [英] How can I use $(this) inside of Fancybox's onComplete event?

查看:953
本文介绍了如何在Fancybox的onComplete事件中使用$(this)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Fancybox的 onComplete 事件中使用jQuery的 $(this),但我遇到麻烦这是我的JavaScript代码:

I'm trying to use jQuery's $(this) inside of Fancybox's onComplete event, but I'm running into trouble. Here's my javascript code:

$('a.iframe').fancybox({  
  centerOnScroll: true,  
  onComplete: function(){  
    var self = $(this);  
    var title = self.title;  
    alert(title.text());  
  }  
});

我已经简化了上面的代码,以得到我的观点,但我实际上会喜欢使用 $(this)由于几个原因,我不会进入这里。

I have simplified the code above to get my point across, but I actually would love to use $(this) for several reasons that I won't go into here.

Fancybox的文档显示了使用这个而不是其文档中的 $(this),但是我没有看到任何例子, code> onComplete 或其他事件。我当然试图使用这个,无济于事。

Fancybox's documentation shows examples of using this instead of $(this)within its documentation, but I didn't see any examples where either were used inside of onComplete or other events. I of course tried using this, much to no avail.

有谁知道如何引用触发的 a.iframe 元素通过使用 $(this)或任何其他方式 onComplete 事件?

Does anyone know how I can reference the triggered a.iframe element by using $(this) or any other means within the onComplete event?

编辑:
我使用 Blackcoat 的建议,这里是最终的语法:

I got this to work using Blackcoat's suggestion, and here's the final syntax that worked:

$('a.iframe').fancybox({
  centerOnScroll: true,
  onComplete: function( links, index ){
    var self = $(links[index]);  
    var title = self.find('.title').text();  
    alert(title); 
   }
});


推荐答案

Bitmanic,

令人遗憾的是,您使用这个没有运气,但您仍然可以引用当前链接。定义你的回调来接受由jQuery选择的链接数组作为第一个参数,索引作为第二个参数:

Sadly, you're out of luck using this, but you can still reference the current link. Define your callback to accept an array of links selected by jQuery as its first parameter, and an index as the second parameter:

  $('a.iframe').fancybox({  
    centerOnScroll: true,  
    onComplete: function( links, index ){  
      var self = $(links[index]);  
      var title = self.title;  
      alert(title.text());  
    }  
  });

Fancybox如何调用onComplete处理程序:

Here's how Fancybox invokes the onComplete hander:

if ($.isFunction(currentOpts.onComplete)) {
    currentOpts.onComplete(currentArray, currentIndex, currentOpts);
}

他们没有使用Javascript的调用应用来调用此函数作为对象的方法。换句话说,这个将引用你的应用程序的全局范围(即文档 object),所以你不能使用它来引用被执行的对象(对它们的羞耻)。相反,它们将三个参数传递给用于指定上下文的回调: currentArray (所选对象), currentIndex currentOpts

They aren't using Javascript's call or apply to invoke this function as a method of an object. In other words, this will refer to the global scope of your application (i.e. the document object), so you can't use it to refer back to the object being acted upon (shame on them). Instead, they pass three parameters to the callback for specifying context: currentArray (the selected object), currentIndex, and currentOpts.

这篇关于如何在Fancybox的onComplete事件中使用$(this)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆