[英]Why does my crypto API return the wrong string?
我想知道為什么下面的 javascript 代碼沒有返回我需要的正確信息。 例如,我想要來自 API 的新聞標題,但它給了我不同的信息。 總而言之,我想定位一個特定的標題,如 [0],但它不會改變。
第二個問題是,是否可以在一個代碼中連接針對不同 HTML 元素的 2 個不同 API,或者您是否必須執行單獨的 javascript 函數?
我正在學習,任何解釋都會有所幫助。
<div class="box" id="insert-news">
<div class="title">Marketplace </div>
<h2>Live News</h2>
<p><span class='highlight'>News Article</span></p>
<p>Information here</p>
</div>
function getData(){
fetch('https://api.coinstats.app/public/v1/news?skip=0&limit=10').then(response => {
return response.json();
}).then(data => {
console.log(data.news[2].title);
let newsTitle ='';
data.news.map((values)=>{
newsTitle = `<div class="title">Marketplace </div>
<h2>Live News</h2>
<p><span class='highlight'>News Article</span></p>
<p>${values.title}</p>
</div>`;
})
document.getElementById('insert-news').innerHTML = newsTitle;
})
}
getData();
我認為您想要達到的是在insert-news
容器中顯示所有新聞
function getData() {
fetch("https://api.coinstats.app/public/v1/news?skip=0&limit=10")
.then((response) => {
return response.json();
})
.then((data) => {
document.getElementById("insert-news").innerHTML = data.news
.map(
(values) =>
`<div class="title">Marketplace </div>
<h2>Live News</h2>
<p><span class='highlight'>News Article</span></p>
<p>${values.title}</p>
</div>`
)
.join("");
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.