2010-07-14 74 views
0

我想要做的是獲取引用a的ID值。我的一個代碼如下:jQuery Fancybox獲取ID值爲

<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.很明顯,我做錯了,所以任何幫助都會成爲頂級!謝謝。

+0

我假設在頁面上有多個標籤與匹配的類? – spinon 2010-07-14 18:54:49

+0

Duplicate:http://stackoverflow.com/questions/2961496/fancybox-get-id-of-clicked-anchor-element。 看看是否有幫助。 – spinon 2010-07-14 19:04:35

+0

是的,有一系列同一類的圖像。我嘗試了你鏈接到的解決方案,但是當Fancybox彈出時它只有「Id單元被點擊4093839」,這樣一串數字就是我以後的ID,但我想把這個ID放到我的formatTitle函數中,具體到我的一個href。 – Rhys 2010-07-14 19:12:15

回答

0

變化

$(this).id

this.id

$(this).attr('id')

,看看是否可行。

編輯:好吧,我測試過這一點,它的工作原理:

使用currentArray[currentIndex].id獲得ID。

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

+0

不行,它只是返回「未定義」。 – Rhys 2010-07-14 19:14:24

+0

我看到在你的函數中,你使用$(this).id和this.id.你有沒有嘗試將它們都改爲$(this).attr('id')?你的意思是什麼回報未定義,是在箱子裏說什麼,或者當你點擊鏈接? – user384918 2010-07-14 19:56:28

+0

這是在框中,這是我正在工作的鏈接:http://bit.ly/9gDA8v – Rhys 2010-07-14 20:13:14