Fancybox 2自定义标题和字幕[来自A(标题)和IMG(alt)

我正在尝试使用带有fancybox 2的图像alt标签中的标题和“字幕”…出于某种原因我似乎无法工作……

$('.fancybox').fancybox({ beforeShow : function() { this.title = '
' + $(this).attr('title') + '
' + $(this).children('img:first').attr('alt') + '
'; }, helpers : { title: { type: 'inner', beforeShow: function(opts) { console.log('beforeShow title helper'); }, }, }, });

来自$(this).attr(’title’)的标题工作,来自$(this)的字幕。儿童(’img:first’)。attr(’alt’)说未定义…我知道我必须遗漏一些简单的东西…

如果我理解正确,你想要fancybox标题作为标签中title属性+ img标签中alt属性的组合。

如果以上是正确的,那么有一个像HTML一样

 alt image 01 

fancybox标题应该说“ title anchor 01 alt image 01

你用这个脚本做到这一点:

 $(document).ready(function() { $(".fancybox").fancybox({ helpers : { title: { type: 'inside'} }, afterLoad: function(){ this.title = this.title + ' ' + $(this.element).find('img').attr('alt'); } }); // fancybox }); // ready 

摆弄调试器我发现这有效:

 beforeShow : function() { var alt = this.element.children[0].alt; this.title = '
' + alt + '
'; },

它不是最优雅的方式,但它可以满足您(和我)的需求 – 从图像中获取ALT值。

问候,保罗

我有一个类似的问题,最终使用单独的标题标签缩略图。 似乎也有效,但不是那么有活力。

  

如果您使用带有Angular ng-repeat Fancybox,您可以执行类似的操作,然后它也适用于文本,您不需要img标记或img alt标记。

 title="{{banner.fancyboxTitle}}
{{ banner.fancyboxParagraph }}"