jquery
  • attributes
  • lightbox
  • 2010-11-14 204 views 0 likes 
    0

    我使用PrettyPhoto燈箱,並試圖在另一非jQuery JavaScript庫,使該鏈接主動設置一個鏈接rel屬性(它設置鏈接類活動的),所以PrettyPhotot燈箱將打開。我不希望鏈接打開,除非它是活動鏈接。我成功設置了該屬性,但每次點擊鏈接時都會打開每個鏈接,而不僅僅是活動鏈接。在FireBug中沒有錯誤。設置rel屬性動態

    Prettyphoto的代碼設置爲這樣:

    $("a[rel^='prettyPhoto']").prettyPhoto(); 
    

    的有效和無效點擊的其他的JavaScript代碼:

    ContentFlowConf: { 
        onclickActiveItem: function (item) { 
         $('.active').attr('rel', 'prettyPhoto[gallery]'); 
        }, 
        onclickInactiveItem: function (item) { 
        $('.active').removeAttr('rel'); 
         $('.item').click(function(event) { 
          event.preventDefault(); 
         }); 
        } 
    } 
    

    最初的HTML是:

    <a class="item" title="Image" href="image.jpg"><img class="content" src="thumb.jpg" alt="Image"/></a> 
    

    其他javascript將html設置爲:

    <a class="item active" title="Image" href="image.jpg"><img class="content" src="thumb.jpg" alt="Image"/></a> 
    

    我當然是新手,不知道什麼會使其工作。我想知道是否需要使用.live函數?

    +0

    哎呀,我忘了我的代碼removeAttr一部分。請參閱編輯。 – Macsupport 2010-11-14 01:06:29

    回答

    0

    解決這個如下:

    onclickActiveItem : function(){ 
        initCBox(); 
    }, 
    
    //lots of code here... 
    
    function initCBox(){ 
        var img1 = $('.item.active').attr('href'); 
        var label = $('.item.active .content').attr('alt'); 
        $.prettyPhoto.open(img1, label); 
    } 
    
    3

    的問題是,一旦你打電話

    $("a[rel^='prettyPhoto']").prettyPhoto(); 
    

    這些鏈接註冊prettyPhoto打開。稍後更改rel屬性不會影響prettyPhoto已設置的事件處理程序。

    我看着docs for prettyPhoto而且似乎還有另一種方式來手動打開圖像:

    $.prettyPhoto.open('image.jpg','Title','Description'); 
    

    因此,當用戶點擊一個鏈接,你可以檢查它是否是積極的,如果是這樣,手動開啓prettyPhoto。這樣的事情可能工作:(未測試)

    $('a.item').click(function() { 
        var link = $(this); 
        if (link.hasClass('active')) 
        $.prettyPhoto.open(this.href, link.text(), ''); 
    }); 
    
    +0

    打敗了我。我會建議刪除或使用手動方法後重新綁定。 +1 – jcolebrand 2010-11-14 01:10:25

    +0

    如果我刪除$('。active')。removeAttr('rel');它適用於第一張圖片,但在此圖片打開後,每張圖片都會在Lightbox中打開。謝謝,更近......。 – Macsupport 2010-11-14 01:14:30

    相關問題