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一样
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 }}"