【问题标题】:jQuery Fancybox get id value of ajQuery Fancybox 获取 a 的 id 值
【发布时间】:2010-07-14 18:48:26
【问题描述】:

我想要做的是获取引用 a 的 ID 值。我的 a 代码如下:

<a class="photoBig" id="<?php echo $photo['id'] ?>" href="<?php echo $f->buildPhotoURL($photo, 'large') ?>" title="<?php echo $photo['title'] ?>">

我正在使用 phpFlickr 分配 ID 以及其他一些东西,效果很好。

所以我使用的打开 Fancybox 的代码是:

$("a.photoBig").attr('rel', 'gallery').fancybox({
            'titleShow'     : true,
            'overlayOpacity' : 0.9,
            'overlayColor' : '#000',
            'autoScale' : false,
            'scrolling' : true,
            'titlePosition' : 'inside',
            'showNavArrows' : true,
            'cyclic' : true,
            'showCloseButton' : false,
            'titleFormat' : formatTitle
        });

然后是函数fomatTitle,即:

function formatTitle(title, id, currentArray, currentIndex, currentOpts) {
    return '<div id="photoBig-title"><span><a href="javascript:;" onclick="$.fancybox.close();"><img src="images/site/closelabel.gif"></a></span>' + (title && title.length ? '<strong>' + title + '</strong>' : '' ) + $(this).id + '<a href="http://www.flickr.com/photos/rustybones/' + this.id + '" title="view original on flickr">view full size</a></div>';
}

除 this.id 外,此函数中的所有内容都有效。显然我做错了,所以任何帮助都是最重要的!谢谢。

【问题讨论】:

  • 我假设页面上有多个匹配类的 a 标签?
  • 重复:stackoverflow.com/questions/2961496/…。看看有没有帮助。
  • 是的,有一系列具有相同类别的图像。我尝试了您链接到的解决方案,但是当 Fancybox 弹出时,它只有“元素 ID 已单击 4093839”,因此该数字字符串是我所追求的 ID,但我想将该 ID 放入我的 formatTitle 函数中,特别是放入我的一个href。

标签: javascript jquery jquery-plugins fancybox


【解决方案1】:

改变

$(this).id

this.id

$(this).attr('id')

看看是否可行。

编辑:好的,我已经测试过了,它可以工作:

使用currentArray[currentIndex].id 获取id。

或者,如果您想要的只是 id 以便您可以使用它来指向您的图像,您可以在其中使用 $(this).attr('href')。

【讨论】:

  • 不行,它只是返回“未定义”。
  • 我看到在你的函数中,你同时使用了 $(this).id 和 this.id。您是否尝试将它们都更改为 $(this).attr('id')?返回未定义是什么意思,是框里说的还是点击链接的时候?
猜你喜欢
  • 2016-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多