2015-12-17 38 views
0

對於這個網站http://eco-tech.bhbcom.net/如何獲得預覽對象

在點擊圖片鏈接,我需要得到預覽圖像的鏈接和下一圖像鏈接的ALT。

look image

如何獲得預覽與同一類對象?

我的代碼:

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

但在執行console.log我已經不確定的。

回答

0

如果我理解你的問題,你正打算這樣的事情...

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

+0

TNX。這是與演示工作,但不是在我的情況。 – Mourat