问题
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