Magnific Popup:来自span的源标题

时间:2013-10-16 12:43:30

标签: javascript magnific-popup

我想从锚标记内的隐藏字幕字段中获取我的Magnific图像的标题 - 而不是标题。这是因为我的标题包含标记。

HTML

<a href="img/zoom.jpg">
  <img src="img/small.jpg" alt=""> 
  <span class="hide">This is a caption with <a href="#">a link</a> in it</span>
</a>

JS

// initialise the magnific lightbox
    $('.js-lightbox').each(function() {
      $(this).magnificPopup({
        delegate: 'a',
        type: 'image',
        tLoading: 'Loading image #%curr%...',
        gallery: {
          enabled: true,
          navigateByImgClick: true,
          preload: [0,1] // Will preload 0 - before current, and 1 after the current image
        },
        image: {
          titleSrc: function(item) {
            return item.el.text;
          },
          tError: '<a href="%url%">The image #%curr%</a> could not be loaded.'
        }
      });
    });

显然return item.el.text;没有按预期工作。感谢。

2 个答案:

答案 0 :(得分:8)

return item.el.find('span').text();

答案 1 :(得分:-3)

谢谢。事实证明这对我有用。虽然我无法让它像工作中那样工作 - 问题在于在span元素中放入一个href。这会导致嵌套的a标记,这在HTML中是非法的。下面的W3C文档与HTML 4有关,但我认为同样适用于HTML 5。

http://www.w3.org/TR/html401/struct/links.html#h-12.2.2

相关问题