编辑:我的意思是分组为同一帖子中的所有REL
提供相同的IMG
属性,但每个帖子都有不同的REL
}作为这个问题末尾的例子。
所以,我需要做以下事情:
<div id="Blog1" class="widget Blog">
<div class="blog-posts hfeed">
<div class="post hentry uncustomized-post-template">
<a name="8829400899632947948"/>
<div class="post-body entry-content">
<div id="8829400899632947948">
<div class="separator">
<a imageanchor="1" href="/images/outta.png">
<img src="/images/outta.png"/></a></div></div>
<div style="clear: both;"/>
</div>
<div class="post-footer">
</div></div></div></div>
我正在使用jQuery和Colorbox。前两个DIV
是帖子容器。我需要使用IMG
属性对每个<div class="post hentry uncustomized-post-template">
中的REL
进行分组,例如:
1 - 发布
1.1 - IMG - REL =“group0”
1.2 - IMG - REL =“group0”
1.3 - IMG - REL =“group0”
1.4 - IMG - REL =“group0”
2 - 发布
2.1 - IMG - REL =“group1”
3 - 发布
3.1 - IMG - REL =“group2”
3.2 - IMG - REL =“group2”
我已尝试过来自jQuery的HAS
,PARENT > CHILDREN
和CHILDREN()
以及来自Colorbox的REL:
,但似乎我缺乏某种逻辑。
有人可以帮助我吗?
答案 0 :(得分:1)
// 'walk' through every element that has class 'post'
// passing the index of each element to variable i
$('.post').each(function(i) {
// on `this` element, find all children img
// to which add attribute 'rel' with a value of
// 'group' concatenated with the parent's index (i)
$(this).find('img').attr('rel', 'group' + i);
});
编辑:添加一些评论,希望这有助于那些新的jquery