Jquery next() for a gallery

早过忘川 提交于 2020-01-06 07:14:05

问题


I'm creating a kind of lightbox gallery by myself but I have problem with loading next and previous images $("a.lightbox").click(function (e) { var a = $(this).next().attr('href'); alert(a); }); As you see this thing doesn't work and I need the href contain of the next element.My HTML is something like this:

<ul class="listing">
        <li><a href="imgs/eli.jpg" id="1" class="lightbox"><img src="thumbs/eli_t.jpg" width="150" height="100" class="images" /></a></li>
        <li><a href="imgs/ggallin.jpg" id="2" class="lightbox"><img src="thumbs/ggallin_t.jpg" width="150" height="100" class="images" /></a></li>
        <li><a href="imgs/jontarata.jpg" id="3" class="lightbox"><img src="thumbs/jontarata_t.jpg" width="150" height="100" class="images" /></a></li>
        <li><a href="imgs/macka s tatuirovki.jpg" id="4" class="lightbox"><img src="thumbs/macka s tatuirovki_t.jpg" width="150" height="100" class="images" /></a></li>
        <li><a href="imgs/mk7.jpg" id="5" class="lightbox"><img src="thumbs/mk7_t.jpg" width="150" height="100" class="images" /></a></li>
        <li><a href="imgs/P5010345.jpg" id="6" class="lightbox"><img src="thumbs/P5010345_t.jpg" width="150" height="100" class="images" /></a></li>
    </ul>

回答1:


You are calling next on your a element. There are no elements that are after your a tag. You could first get the parent, get the next li and then grab the a from its children.

var a = $(this).parent().next().children('a').attr('href');



回答2:


How about:

var a = $(this).parent().next().children('a').attr('href');

You have to navigate up to the parent, move to the next li and then get the link.

You'll also want to add logic in to deal with reaching the end (and beginning) of the list.



来源:https://stackoverflow.com/questions/5599079/jquery-next-for-a-gallery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!