【问题标题】:Magnific Popup - Index errorMagnific Popup - 索引错误
【发布时间】:2015-12-18 15:23:25
【问题描述】:

我的 Magnific Popup 无法使用 index 选项。我的目标是让画廊在点击图像的索引上打开。在我的 HTML 中,每张图片都有这个:

<div class="open_gallery_index" data-id=5><img ... ></div>

那么我的 JS 看起来像这样:

$(function(){
  $('.open_gallery_index').click(function(){
    var i = Number($(this).data("id"));
    $.magnificPopup.open({
      items: g_items,
      gallery: {
        enabled: true
      },
      type: 'image'
    },i);
  });
})

但是点击图片时出现错误

未捕获的类型错误:无法读取未定义的“已解析”属性

虽然如果我手动将变量 i 设置为 5,一切正常,所以问题一定出在这个

var i = Number($(this).data("id"));

但是我已经尝试了一切都没有成功,感谢您的帮助。

【问题讨论】:

  • 您是否尝试过像这样将您的 5 用引号括起来?
  • $(this).data("id") 为空
  • 你试过parseInt()而不是Number()
  • 我试过 parseInt() 结果是一样的

标签: javascript jquery magnific-popup


【解决方案1】:

像这样更新您的 data-id 属性。

<div class="open_gallery_index" data-id="5"><img ... ></div>

【讨论】:

    【解决方案2】:

    我认为$.magnificPopup.open 需要src 属性。不太确定 g_items 在您的代码中是什么,但请尝试以下操作:

    $.magnificPopup.open({
      items: {
        src: '.open_gallery_index',
        type: 'image'
      }
      gallery: {
        enabled: true
      },
    },i);
    

    【讨论】:

    • g_items 是带有srctime 数组的对象,应该没问题
    【解决方案3】:
    $.magnificPopup.open({
      items: g_items,
      gallery: {
        enabled: true
      },
      type: 'image'
    },i);
    i--;
    i++;
    

    它以这种方式与我一起工作,我不知道为什么

    【讨论】:

      猜你喜欢
      相关资源
      最近更新 更多
      热门标签