【问题标题】:How to get ID of link that opened Fancybox3 within onComplete?如何获取在 onComplete 中打开 Fancybox3 的链接 ID?
【发布时间】:2017-03-12 06:26:02
【问题描述】:

我在 stackoverflow 上的其他问题中看到了似乎可以解决此问题的方法,但没有一个有效。

我正在尝试获取单击以打开 Fancybox 的链接的内部文本:

$("a.turnDateLink").each(function() {

              var that = $(this);

              $( "a.turnDateLink" ).fancybox({
                  'type': 'modal',
                  'onComplete': function() {

                      var currentday = $(that).text();
                      console.log(currentday);
                  },
                  'afterClose': clearCurrentDay,
                  'fullScreen' : false
              });

});

它每次只返回最后一个a.turnDateLink.的innerText!呃。

请记住,我想使用 fancybox 分组。

请参阅 Fancybox 文档以了解分组:http://fancyapps.com/fancybox/3/docs/#usage

如果你有一组物品,你可以使用相同的属性 data-fancybox 值为他们每个人创建一个画廊。每组 应该有一个唯一的值:

【问题讨论】:

    标签: javascript jquery callback fancybox fancybox-3


    【解决方案1】:

    查看有关事件的文档 - http://fancyapps.com/fancybox/3/docs/#events

    第一个示例包含有用的提示,包括如何找到被点击的元素:

    onComplete: function( instance, slide ) {
    
        // Tip: Each event passes useful information within the event object:
    
        // Object containing references to interface elements
        // (background, buttons, caption, etc)
        // console.info( instance.$refs );
    
        // Current slide options
        // console.info( slide.opts );
    
        // Clicked element
        // console.info( slide.opts.$orig );
    
        // Reference to DOM element of the slide
        // console.info( slide.$slide );
    
    }
    

    【讨论】:

      【解决方案2】:

      像这样尝试。将$( "a.turnDateLink" ) 更改为that。

      $("a.turnDateLink").each(function() {
      
                        var that = $(this);
      
                        that.fancybox({
                            'type': 'modal',
                            'onComplete': function() {
                                $("#currentday").html('');
      
                                var currentday = that.text();
                                console.log(currentday);
                            },
                            'afterClose': clearCurrentDay,
                            'fullScreen' : false
                        });
          });
      

      【讨论】:

      • 这样做的问题是你不能在这样做时使用fancybox分组。这会导致每个实例使用不同的花式框,并且您无法对它们进行分组。这非常接近,但我希望使用分组。
      • 我不明白分组意味着您的预期输出是什么?
      • 请看我修改后的问题。 Fancybox 文档中提到了分组。感谢您的宝贵时间!
      猜你喜欢
      • 2020-12-22
      • 1970-01-01
      • 1970-01-01
      • 2016-08-29
      • 2017-08-03
      • 1970-01-01
      • 2011-02-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多