我有餐馆名单,我必须附上每家餐馆的图片,这些图片来自数据库。为此,我让div成为动态但是图片没有附加在那个div上。如果我将div的id设为静态,那么它会显示来自服务器的图片。问题在于使div id动态化。请帮我。这是我的示例代码:
$("#list").empty();
$.each(data, function (i, item) {
if (data[i].has_logo == 1) {
$('#pic' + i).css("background-image", "url('http://www.thefoodhive.com/web_root/provider_uploads/" + data[i].id + "/logo.jpg')");
}
if (data[i].has_menu == 0) {
status = 'Call Now';
} else {
status = 'Order Now';
}
if (data[i].from_time == '' || data[i].from_time == null) {
data[i].from_time = 'NA';
data[i].to_time = '';
}
content = '<div ><div class="container"><div id="pic' + i + '" class="left"></div><div class="right"><div class="rightno">' + data[i].name + '</div></h4></p><i>Address:' + data[i].address + ',Timing:' + data[i].from_time + '-' + data[i].to_time + '</i><br><center><div style="float: right;"></div></div> <div class="bottom"><div class="btnleft" style="float: left;"><input class="callbttn" value="CALL NOW"type="button"></input></div><div class="btnright" style="float: left;"><input class="callbttn" value="' + status + '" type="button" onclick="getdata(\'' + data[i].id + '\',\'' + data[i].has_menu + '\',\'' + data[i].phone_no + '\');"></input></div></div>';
答案 0 :(得分:0)
您正在将样式应用于尚不存在的div ......
尝试应用内联背景,如下所示:
content = '<div ><div class="container"><div id="pic' + i + '"
class="left" style="background-image:url(http://www.thefoodhive.com/web_root/provider_uploads/' + data[i].id + '/logo.jpg">...