gpt4 book ai didi

javascript - 在 JavaScript 对象中附加带有 src 的 img 标签?

转载 作者:行者123 更新时间:2023-12-02 14:30:34 24 4
gpt4 key购买 nike

我正在尝试从 Javascript 对象中获取添加图像并将其包装在 img 标签中 - 如何正确执行此操作?仅添加 img 作为元素不起作用,那么最好的方法是什么?

fiddle :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)
})

最佳答案

<img /> 的 URL应放在 src 中属性,并且该元素是自闭合的;它没有单独的结束标签。试试这个:

div.append('item <img src="' + v.imageURI + '" /><span class="art">' + v.artno + '</span><span class="price">' + v.price + '</span>')

Updated fiddle

另请注意,您不需要将字符串文字附加在一起,您可以只使用单个字符串。

关于javascript - 在 JavaScript 对象中附加带有 src 的 img 标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37875263/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com