Jquery 如何获取预览对象

Jquery 如何获取预览对象,jquery,jquery-selectors,Jquery,Jquery Selectors,对于此站点 单击图像链接,我需要获得预览图像链接和下一个图像链接的ALT 如何使用同一类获取预览对象 我的代码: $(this).prev('.colorbox-iframe').find('img').attr('alt'); $(this).next('.colorbox-iframe').find('img').attr('alt'); 但是在console.log中我没有定义。如果我理解你的问题,你打算这样做 <div> <img alt="x" src="ht

对于此站点

单击图像链接,我需要获得预览图像链接和下一个图像链接的ALT

如何使用同一类获取预览对象

我的代码:

$(this).prev('.colorbox-iframe').find('img').attr('alt');
$(this).next('.colorbox-iframe').find('img').attr('alt');

但是在console.log中我没有定义。

如果我理解你的问题,你打算这样做

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

$(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);
    });
  });
});