【发布时间】:2019-05-10 06:39:07
【问题描述】:
我正在尝试让我的代码将 api 对象输出到 html 文件。
const container = document.createElement('div');
container.setAttribute('class', 'container');
obj = fetch('https://apis.is/concerts')
.then(function(response) {
return response.json();
})
.then(function(data) {
return obj = data;
})
.then(() => idk())
function idk() {
let count = 0;
for(key in obj.results) {
count++;
};
console.log(count);
for(let i = 0; i < count; i++) {
const card = document.createElement('div');
card.setAttribute('class', 'card');
const h1 = document.createElement('h1');
h1.textContent = obj.results[i].eventDateName;
const p = document.createElement('p');
p.textContent = obj.results[i].dateOfShow;
container.appendChild(card);
card.appendChild(h1);
card.appendChild(p);
};
};
我一直在尝试使用 DOM 为 html 文件创建元素,但好像有些代码被忽略了。
【问题讨论】:
-
那会是哪一段代码?
-
idk函数
-
我看不到您将容器附加到 DOM 的位置?
标签: javascript html fetch-api