2016-08-04 38 views
0

使用jquery append函数添加图像的src。我在foreach循环中使用一个变量,我想追加图像src。我怎么做。如何使用jquery append函数添加图像的src?

我jQuery代码

$.each(data.products, function(key,val) { 
    $("div#yourwishlist").append('<div class="col-sm-6 col-md-4"><article class="box"><figure><a class="hover-effect" title="" href="#"><img width="300" height="160" alt="" src="'+val('thumb')+'"></a></figure><div class="details"><a class="pull-right button uppercase" href="" title="View all">select</a><h4 class="box-title">'+val['name']+'</h4><label class="price-wrapper"><span class="price-per-unit">$170</span>avg/night</label></div></article></div>'); 
console.log(val['name']); 
}); 

这个代码是不是在我的案子......

+0

squere括号这里' val('thumb')',类似于'val ['thumb']' –

+0

更改val('thumb')至val ['thumb'] –

+0

当我提醒val ['thumb']时。它显示正确的值......但我想我正在写一些语法错误在src =“'+ val('thumb')+'”。因为我认为它没有显示src =“http://placehold.it/300x160” –

回答

1

亚太区首席技术官Matt你可以像src属性添加到图像:

$.each(data.products, function(key,val) { 
    $("div#yourwishlist").append('<img width="300" height="160" alt="" src="'+ val +'">'); 
}); 

// here val contains the path of image in it