Fancybox beforeLoad Callback: Javascript, jQuery and referencing $this?

守給你的承諾、 提交于 2019-11-29 17:25:38

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
});

When you did this:

'beforeLoad': add_visited($(this)),

$(this) became a parameter to the function. So use the parameter -- this has no context in the function.

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

Or, alternatively, you could do this instead:

$('.clinical_trial_link').fancybox({
    'beforeLoad': function(){
        $(this).addClass('visited');
    },

For more on this, see this article.

Ankit Sewadik

Use this is in your fancybox parameters:

'beforeLoad': function() { 
     alert('test'); 
},
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!