2012-07-19 38 views
0

有没有一种方法可以从<img>标签取代标签而不是<a>标签?来自img标签的Fancybox标题不是href

例如 HTML:

<a rel="fancybox" href="#"> 
<img src="ball.jpg" title="this is the caption i want to show in fancybox" alt="alt text here" /> 
</a> 

<a rel="fancybox" href="#"> 
<img src="ball2.jpg" title="this is the caption2" alt="alt text here" /> 
</a> 

<a rel="fancybox" href="#"> 
<img src="ball3.jpg" title="this is the caption 3" alt="alt text here" /> 
</a> 

我已经试过,但它不工作: JQuery的:

  $("a[rel=fancybox]").fancybox({ 
       'transitionIn': 'elastic', 
       'transitionOut': 'elastic', 
       'titlePosition': 'inside', 
       'titleFormat': function (title, currentArray, currentIndex, currentOpts) { 
        title = $(this).find("img").attr("title"); 
        return '<span>' + title + '</span>'; 
       } 
      }); 
+0

什么版本的fancybox的API选项onStarttitle属性?因为您正在使用v1.3.2 +和v2.x的选项,这些选项彼此不兼容。 – JFK 2012-07-19 06:54:47

+0

jquery.fancybox-1.3.4.js – ShaneKm 2012-07-19 06:58:34

回答

1

有关的fancybox V1.3.4只需使用其中基本上你设定的img标签(不是title属性)像

alt属性的fancybox称号API选项 titleFromAlt
<a rel="fancybox" href="#"> 
<img src="ball.jpg" alt="this is the caption i want to show in fancybox" /> 
</a> 

那么你的脚本:

  $("a[rel=fancybox]").fancybox({ 
       'transitionIn' : 'elastic', 
       'transitionOut': 'elastic', 
       'titlePosition': 'inside', 
       'titleFromAlt' : true 
      }); 

你可以了解更多有关this option and how title works in fancybox v1.3.2+ here

#EDIT:强制读取来自img标签使用像

 $("a[rel=fancybox]").fancybox({ 
      'transitionIn' : 'elastic', 
      'transitionOut': 'elastic', 
      'titlePosition': 'inside', 
      'onStart' : function(currentArray,currentIndex){ 
       var obj = currentArray[ currentIndex ]; 
       this.title = $(obj).find('img').attr("title"); 
      } 
     }); 
+0

但是对于搜索引擎优化的目的,我需要它从图像的“标题”标签中拉出来 – ShaneKm 2012-07-19 07:11:36

+0

看到我的编辑从'img'标签获得'title'属性 – JFK 2012-07-19 07:24:16

+0

你做到了!谢谢! – ShaneKm 2012-07-19 07:29:37

0

试试这个:

$("img[title=fancybox]").fancybox({ 
      'transitionIn': 'elastic', 
      'transitionOut': 'elastic', 
      'titlePosition': 'inside', 
      'titleFormat': function (title, currentArray, currentIndex, currentOpts) { 
       title = $(this).find("img").attr("title"); 
       return '<span>' + title + '</span>'; 
      } 
     }); 
+0

$(“a [rel = fancybox]”)。fancybox({HAS is for the caller for other reasons。 – ShaneKm 2012-07-19 06:50:10

0

试试这个 -

 $("a[rel=fancybox]").fancybox({ 
      'transitionIn': 'elastic', 
      'transitionOut': 'elastic', 
      'titlePosition': 'inside', 
      'titleFormat': function (title, currentArray, currentIndex, currentOpts) { 
       title = $(currentArray[currentIndex]).find('img').attr('title'); 
       return '<span>' + title + '</span>'; 
      } 
     }); 

或者你可以传递参数'titleFromAlt' : true

+0

does not work ... – ShaneKm 2012-07-19 07:06:30

+0

答案已更新。试试,它会得到标题 – 2012-07-19 07:16:51

+0

@YograjGupta:你在钓鱼吗?你正在复制其他人的帖子的答案...这就是所谓的剽窃。 – JFK 2012-07-19 07:28:35