.load() 不适用于每个对象
我正在尝试从子页面加载内容,然后为 PrettyPhoto 插件所需的图库添加 rel 属性。脚本运行后,rel 仅添加到最后一组图像。\
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;
});
});
}
I am trying to load content in from sub pages and then add the rel attribute for a gallery required by the prettyPhoto plug in. After the script runs the rel has only been added to the last set of images.\
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;
});
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您使用
each
进行迭代时,您将在每次迭代中添加一个:
然后在其正下方:
在第一次迭代中,
$('.album').length
将是一,第二个它将是二,依此类推。最终迭代将最终调用.load('test.html .prettyphotoalbum p > *', ...
在您添加的所有三个元素上,
我认为您只想绑定
load
。到您刚刚创建的东西:此外,您只需要一个
.live
调用,因此将其移至.each
之外,因此这样的操作应该效果更好:As you iterate using
each
, you're adding a<div class="album">
on each iteration:And then right below that:
On the first iteration,
$('.album').length
will be one, on the second it will be two, etc. The final iteration will end up calling.load('test.html .prettyphotoalbum p > *', ...
on all three<div class="album">
elements that you've added.I think you want to just bind the
load
to the thing you just created:Also, you only need one
.live
call so move this:outside the
.each
. So something like this should work better: