如果我理解你的問題,你正打算這樣的事情...
<div>
<img alt="x" src="http://lorempixel.com/100/100" />
</div>
<div>
<img alt="y" src="http://lorempixel.com/100/100" />
</div>
<div>
<img alt="z" src="http://lorempixel.com/100/100" />
</div>
的jQuery:
$(function() {
$('div img').each(function() {
$(this).click(function() {
var alt = $(this).attr('alt');
var alt_prev = $(this).closest('div').prev('div').children('img').attr('alt');
var alt_next = $(this).closest('div').next('div').children('img').attr('alt');
if (alt_prev === undefined)
alt_prev = $('div:last img').attr('alt');
if (alt_next === undefined)
alt_next = $('div:first img').attr('alt');
console.log('Clicked alt: ' + alt + ' Prev alt: ' + alt_prev + ' Next alt: ' + alt_next);
});
});
});
https://jsfiddle.net/jonathanzuniga/rp5mux4m/embedded/result/
TNX。這是與演示工作,但不是在我的情況。 – Mourat