我尝试从Javascript对象添加图像并将其包装在img标记中 - 如何正确执行此操作?仅添加img作为元素并不起作用,所以最好的方法是什么?
小提琴:https://jsfiddle.net/hszxbmrx/6/
Javascript对象:
var retailerData = {
"del": {
"zip": "",
"city": ""
},
"user": {
"country": "",
"phone": "",
"nbrOrders": 0,
"name": "",
"salesPerson": "",
"customerNo": "",
"email": ""
},
"order": {
"shippingSum": 0.0,
"orderno": "0",
"voucher": "",
"currency": "SEK",
"orderVat": 3322.5,
"orderSum": 13290.0,
"items": [{
"qtyAvail": 0,
"imageURI":"http://www.windowspasswordsreset.com/windows-password-knowledge/images/dell-laptop.jpg",
"price": 6295.0,
"qty": 1,
"id":"244992",
"artno": "DEL-17812033.10-4",
"label": "E7240/i5-4310U/4GB1/128SSD/12,5HD(1366x768)/W7P 3-Cell/CAM/3YRNBD/W8.1P/US int Keyboard",
"category": "Computers - Notebooks",
"manufacturer": "Dell"
}, {
"qtyAvail": 31,
"imageURI":"http://www.windowspasswordsreset.com/windows-password-knowledge/images/dell-laptop.jpg",
"price": 6995.0,
"qty": 1,
"artno": "20BV001KUK",
"label": "Lenovo ThinkPad T450 20BV - 14" - Core i3 5010U - 4 GB RAM - 500 GB Hybrid Drive",
"category": "Computers - Notebooks",
"manufacturer": "Lenovo"
}]
}
}
脚本:
$.each(retailerData.order.items,function(i,v){//get the item
var div = $('<div class="test">')
div.append('item '+ '<img>'+ v.imageURI+'</img>' + '<span class="art">'+ v.artno+'</span>' + '<span class="price">'+ v.price+'</span>' )
$('.carttable').append(div)
})
答案 0 :(得分:2)
<img />
的网址应放在src
属性中,该元素是自动关闭的;它没有单独的结束标记。试试这个:
div.append('item <img src="' + v.imageURI + '" /><span class="art">' + v.artno + '</span><span class="price">' + v.price + '</span>')
另请注意,您不需要将字符串文字附加在一起,您只需使用单个字符串。