2013-02-16 176 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}); 

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