[英]Append img tag with src from a JavaScript object?
I'm trying fetch to add an image from a Javascript Object and wrap it in an img tag - how do I do this correctly? 我正在尝试从Javascript对象获取图像并将其包装在img标签中-如何正确执行此操作? Adding just img as an element doesn't work so whats the best way to do this?
仅添加img作为元素是行不通的,那么实现此目的的最佳方法是什么?
Fiddle: https://jsfiddle.net/hszxbmrx/6/ 小提琴: https : //jsfiddle.net/hszxbmrx/6/
Javascript Object: 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"
}]
}
}
Script: 脚本:
$.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)
})
The URL of the <img />
should be placed in the src
property, and the element is self closing; <img />
的URL应该放在src
属性中,并且该元素是自动关闭的。 it does not have a separate closing tag. 它没有单独的结束标记。 Try this:
尝试这个:
div.append('item <img src="' + v.imageURI + '" /><span class="art">' + v.artno + '</span><span class="price">' + v.price + '</span>')
Also note that you don't need to append string literals together, you can just use a single string. 还要注意,您不需要将字符串文字附加在一起,只需使用单个字符串即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.