我在 JSON 中有图像 url,如下所示:
var obj = [
{
country: "SG",
images: "../images/flag/singapore.jpg"
},
{
country: "TH",
images: "../images/flag/thailand.jpg"
}
]
function render() {
return `
<select name="sourcecountrycode" id="sourcecountrycode">
${obj.map((cn) => `
<option value=${cn.country}>${cn.country}</option>`)}
// here options SG, TH are generated in select tag dynamically
</select>
`
}
$('#block').html(render());
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="block">
</div>
如何使用 JavaScript 将图像添加到选择中的相应选项?