【发布时间】:2021-01-22 21:47:32
【问题描述】:
奇怪的是,当我使用 div 元素作为父元素,然后在其中嵌套其他 div 时,会显示图像。当我单击检查时,我可以看到图像在那里。控制台没有通过错误,所以我真的不知道我可能哪里出错了
const url = "https://restcountries.eu/rest/v2/all";
const select = document.querySelector("select");
fetch(url)
.then(res => res.json())
.then(data => data.forEach(countryApp))
function countryApp(myData) {
let option = document.createElement("option");
let ctn = document.createTextNode(myData.name);
option.appendChild(ctn);
let img = document.createElement("img");
img.setAttribute("src", myData.flag);
img.setAttribute("alt", "country flag");
img.setAttribute("width", 20);
img.setAttribute("height", 20);
option.appendChild(img);
select.appendChild(option)
}
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Practise App</title>
<link rel="stylesheet" href="index.css">
</head>
<body>
<select class="" name=""></select>
<script src="index.js" type="text/javascript"></script>
</body>
</html>
【问题讨论】:
-
谢谢。我就是这么想的,但我不确定
标签: javascript html image