Fancybox beforeLoad回调:Javascript,jQuery和引用$ this? [英] Fancybox beforeLoad Callback: Javascript, jQuery and referencing $this?

查看:378
本文介绍了Fancybox beforeLoad回调:Javascript,jQuery和引用$ this?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何完成我想要做的,这在开始时,看起来很简单。我想在将链接的内容调用ifcame之前添加'visited'类。

I'm not sure how to accomplish what I'm looking to do, which, on starting out, seemed simple. I want to add a 'visited' class to a clicked link before calling the linked content into a fancybox iframe.

Fancybox工作正常,只要我离开 beforeLoad 调用。

Fancybox works fine, as long as I leave off the beforeLoad call. When added, the page simply reloads the window, bypassing the Fancybox.

我不确定我的 beforeLoad 函数(这里称为 add_visited )允许使用jQuery(如在 addClass 中),或者是否需要坚持直接的javaScript功能( element.setAttribute(className,visited))。在这里粘贴的代码是用于jQuery,引用 中的 c> c>。

I'm not sure whether my beforeLoad function (here called add_visited) is allowed to use jQuery (as in addClass), or whether I need to stick to straight javaScript functionality (element.setAttribute("className", "visited")). The code pasted here is for jQuery, with a reference to a passed this in theClicked.

所以,你可以看到,我也不知道如何引用 $(this)功能。基本上我想从实际的 $。fancybox()调用中结转 this

So, as you can see, I'm also not sure how to refer to $(this) in the function. Essentially I want to carry over this from the actual $.fancybox() call.

为了清楚起见, .clinical_trial_link 是应用于锚点的类。在实际代码中, a.clinical_trial_link

And, for clarity, .clinical_trial_link is a class applied to an anchor. In the actual code it's a.clinical_trial_link.

为了更清楚起见,我在这里讨论了几个对话该方法将标记链接视为已访问的相对优点。我得到的缺点是简单地使用一个类,但在一个网站作为链接重的这一个,看到已经点击已经是必要的。

For further clarity, I've read a few conversations here discussing the relative merits of this method of marking links as visited. I get the downside to simply using a class, but on a site as link heavy as this one, seeing what's been clicked already is imperative.

最后,位于此处: http://pull4parkinsonsfoundation.org/clinical_trials/

当然,js会从这里粘贴的变化,我会继续像一个键盘的mackeral thrash。 ; - )

The js, of course, will change from what's pasted here as I continue to thrash around like a mackeral with a keyboard. ;-)

任何帮助都将不胜感激,并提前感谢您在 stackoverflow 上所做的出色工作。这是我第一次在几年后 发布 一个问题!

Any help would be appreciated, and thanks in advance for the excellent work you all do on stackoverflow. This is the first time I've actually had to post a question in years!

function add_visited(theclicked) {
    $(this).addClass('visited');
}

$(document).ready(function() {
    $('.clinical_trial_link').fancybox({
        'beforeLoad': add_visited($this),
        'maxWidth': 1222,
        'maxHeight': 1222,
        'fitToView': true,
        'width': '80%',
        'height': '90%',
        'topRatio': 0.2,
        'autoSize': false,
        'closeClick': false,
        'openEffect': 'elastic',
        'closeEffect': 'elastic'
    });
});


推荐答案

如果你想做的是添加 class 绑定到fancybox的选择器,在任何fancybox回调中使用 $(this.element)

If what you want to do is to add a class to the selector bound to fancybox, refer to that element using $(this.element) within any of the fancybox callbacks like

$('.clinical_trial_link').fancybox({
  'beforeLoad': function(){
    $(this.element).addClass("visited");
  },
  'maxWidth': 1222,
  // other API options etc
});

这篇关于Fancybox beforeLoad回调:Javascript,jQuery和引用$ this?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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