2011-09-07 19 views
0

私はサブページからコンテンツを読み込み、prettyPhotoプラグインに必要なギャラリーのrel属性を追加しようとしています。画像の最後のセット。\.load()はすべてのオブジェクトに適用されません。

HTML

<div class="prettyphotothumb"> 
    <ul class="navsub"> 
    <li><a href="album.html"><img src="images/thumbnail.jpg" alt="" />Album</a></li> 
    <li><a href="blarg.html"><img src="images/thumbnail.jpg" alt="" />Blarg</a></li> 
    <li><a href="test.html"><img src="images/thumbnail.jpg" alt="" />Test</a></li> 
    </ul> 
</div> 

JS

if($('div.prettyphotothumb').length > 0) { 
    $('div.prettyphotothumb a').each(function() { 
     var pageLink = $(this); 
     var albumTitle = $(pageLink).text(); 
     var album = $('<div class="album"></div>').appendTo($(pageLink).parent()); 

     $('.album').load(this.href+' .prettyphotoalbum p > *',null,function(){ 
      album.children('a').attr('rel','prettyPhoto['+albumTitle+']'); 
     }); 

     $('a[rel^="prettyPhoto"]').live("click",function() { 
      $.prettyPhoto.open($(this).attr("href"),"",""); 
      return false; 
     });   

    }); 
} 

答えて

2

あなたが<div class="album"> Oを追加しているeachを、使用して反復したようnはそれぞれの反復:

右それ以下
var album = $('<div class="album"></div>').appendTo($(pageLink).parent()); 

そして:最初の繰り返しで

$('.album').load(//... 

$('.album').length一つになり、それは2になり第二に、等最後の反復が終わるだろう追加した3つのすべての要素<div class="album">.load('test.html .prettyphotoalbum p > *', ...を呼び出します。私はあなたがちょうどあなたが作成したものにloadをバインドしたいと思います

.each

$('a[rel^="prettyPhoto"]').live("click",function() { 
    $.prettyPhoto.open($(this).attr("href"),"",""); 
    return false; 
}); 

// Use album rather than $('.album') 
album.load(this.href+' .prettyphotoalbum p > *',null,function(){ 
    album.children('a').attr('rel','prettyPhoto['+albumTitle+']'); 
}); 

はまた、あなただけので、これを動かす1 .liveコールを必要としています。したがって、このようなものはうまくいくはずです:

if($('div.prettyphotothumb').length > 0) { 
    $('div.prettyphotothumb a').each(function() { 
     var pageLink = $(this); 
     var albumTitle = $(pageLink).text(); 
     var album  = $('<div class="album"></div>').appendTo($(pageLink).parent()); 

     album.load(this.href+' .prettyphotoalbum p > *', null, function() { 
      album.children('a').attr('rel', 'prettyPhoto[' + albumTitle + ']'); 
     }); 
    }); 

    $('a[rel^="prettyPhoto"]').live("click", function() { 
     $.prettyPhoto.open($(this).attr("href"), "", ""); 
     return false;  
    }); 
} 
関連する問題