2 回答

TA貢獻(xiàn)2019條經(jīng)驗(yàn) 獲得超9個(gè)贊
好吧,您創(chuàng)建了元素,但您仍然需要將它添加到 DOM。
要?jiǎng)?chuàng)建您的元素:
const title = document.createElement('h1')
并將其添加到 DOM(使其實(shí)際出現(xiàn)在您的頁(yè)面上):
document.body.appendChild(title)
但是現(xiàn)在您仍然需要從 API 中添加您的實(shí)際標(biāo)題:
title.innerText = articles[i].title
一起來(lái):
const title = document.createElement('h1') // create the heading
title.innerText = articles[i].title // add the title from the API to your heading
document.body.appendChild(title) // add your heading to the DOM (this will make it appear)

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超8個(gè)贊
看看這個(gè)。我回答了你的騙局,但這里更好
let html = [];
fetch('https://api.nytimes.com/svc/topstories/v2/science.json?api-key=yourApiKey')
.then((resp) => resp.json())
.then(function(data) {
data.results.forEach(res => {
html.push(`<h1>${res.title}</h1>`);
})
document.getElementById("res").innerHTML = html.join("");
})
<div id="res"></div>
添加回答
舉報(bào)