2013-02-16 184 views
0

想要將圖像鏈接到外部鏈接,我會說它是在.wrap中,但似乎無法工作。我檢查了大部分問題,但我嘗試的所有內容都無濟於事。如何將外部鏈接添加到圖片?

var photos = [ 
{ file: 'image1.png', tag: 'search', href: 'http://www.googl.com'}, 
{ file: 'image2.png', tag: 'search', href: 'http://www.bing.com'}, 
{ file: 'image3.png', tag: 'search', href: 'http://www.yahoo.com' } 
]; 

var photoCount = photos.length; 
var pageItems = []; 
for (var i = 0; i < photoCount; i++) { 
    if (photos[i].tag != tag && tag != 'all') { 
     continue; 
    } 
    var img = $('<img></img>') 
    .attr('src', 'static/images/tv/' + photos[i].file) 
    .addClass('slider-photo'); 
    pageItems.push({item: img,caption: descDiv}); 
} 
+0

「* ...我會說它是在一個.wrap,但似乎無法開始工作... *,」可以你解釋一下自己? – Alexander 2013-02-16 18:23:18

+0

photos [i] .tag!= tag && tag!='all'...所有的項目都會'繼續' – Shanimal 2013-02-16 18:24:46

+1

我認爲你沒有聲明標籤變量.. – sasi 2013-02-16 18:28:59

回答

0
var photos = [ 
    { file: 'image1.png', tag: 'search', href: 'http://www.googl.com'}, 
    { file: 'image2.png', tag: 'search', href: 'http://www.bing.com'}, 
    { file: 'image3.png', tag: 'search', href: 'http://www.yahoo.com' } 
]; 

$.each(photos,function(k,v){ 
    $('<img />') 
     .attr('src', '../static/images/tv/' + v.file) 
     .addClass('slider-photo') 
     .append('body') 
     .click(function(){ 
      document.location.href = v.href; 
     }) 
}) 

http://codepen.io/anon/pen/npsAz

+0

請注意,您不會在那裏看到圖像,因爲它們與codepen.io中的根相關......用絕對路徑替換那些圖像,並且圖像應顯示並鏈接到適當的位置。 – Shanimal 2013-02-16 18:39:33

0

只需添加一個<a>標籤周圍..

var img = $('<img></img>') 
.attr('src', 'static/images/tv/' + photos[i].file) 
.addClass('slider-photo'); 
var finalOutput='<a href="externallink">'+img+'<a>'; //<-----here 
pageItems.push({item: finalOutput,caption: descDiv}); 

但是..希望你有一些其他的代碼也一樣,即追加生成的圖像文件中..