使用jquery append函数添加图像的src。我在foreach循环中使用变量,我想追加图像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 :(得分:1)
Hitesh您可以将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