문제

In a page in which I have two or more unordered lists of links at which each link links to an image.

As these unordered lists are generated from a database I would like to get fancybox to make one gallery from each unordered list. Do you hve an idea how to put the same rel attribute to all the links of each list with the help of jQuery?

It looks like this:

<ul>
<li><a href=""><thumb img/></a></li>
<li><a href=""><thumb img/></a></li>
<li><a href=""><thumb img/></a></li>
</ul>

<ul>
<li><a href=""><thumb img/></a></li>
<li><a href=""><thumb img/></a></li>
<li><a href=""><thumb img/></a></li>
</ul>

But it should with the help of jQuery look like this:

<ul>
<li><a rel="gallery1" href=""><thumb img/></a></li>
<li><a rel="gallery1" href=""><thumb img/></a></li>
<li><a rel="gallery1" href=""><thumb img/></a></li>
</ul>

<ul>
<li><a rel="gallery2" href=""><thumb img/></a></li>
<li><a rel="gallery2" href=""><thumb img/></a></li>
<li><a rel="gallery2" href=""><thumb img/></a></li>
</ul>

I managed it to look like this:

<ul>
<li><a rel="gallery1" href=""><thumb img/></a></li>
<li><a rel="gallery2" href=""><thumb img/></a></li>
<li><a rel="gallery3" href=""><thumb img/></a></li>
</ul>

<ul>
<li><a rel="gallery4" href=""><thumb img/></a></li>
<li><a rel="gallery5" href=""><thumb img/></a></li>
<li><a rel="gallery6" href=""><thumb img/></a></li>
</ul>

With this code:

$("ul a").attr(
   "rel", function( index ){
      return( "gallery" + (index + 1) );
    }
);

But this doesn't help me. :(

도움이 되었습니까?

해결책

Try this code:

$("ul").each(function(index, element) {
    $(this).find("li").find("a").attr("rel","gallery"+(index+1));
});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top