javascript - Displaying API information after a button push
问题描述
So I am trying to display the information for a random quote from an API after they click a button on screen. However, the button displays, and the information is run in the console when it loads on the page, but it does not display on the page at all yet, even though I set it up to display immediately when the page loads. Any ideas on how I can fix this?
const output = (quoteInfo) => {
quoteInfo.forEach(
quotes => {
let article = document.createElement('article');
let anime = document.createElement("h4");
anime.textContent = quotes.anime;
let quote = document.createElement("h3");
quote.textContent = quotes.quote;
let character = document.createElement("h4");
character.textContent = quotes.character;
article.appendChild(anime);
article.appendChild(quote);
article.appendChild(character);
document.querySelector("#quote").appendChild(article);
}
);
}
const URL = "https://animechan.vercel.app/api/random"
const buttonElement = document.getElementById("submitButton");
fetch("https://animechan.vercel.app/api/random")
.then(response => response.json())
.then(quotes => console.log(quotes))
//const reset = () => {
//document.querySelector("#quote").innerHTML = " ";
//}
//buttonElement.addEventListener("click", reset())
解决方案
它的Yaboi123,我认为您在脚本中缺少加载到窗口的事件。您可以使用window.addEventListener()
采用两个必需参数的方法来做到这一点。第一个是event
,第二个是返回值callback
的函数。
我假设您的 API 正在返回一个类似这样的数组:
let info = [
{
anime: "Fate/zero",
character: "Rider",
quote: "To win, but not destroy"
},
{
anime: "Hai to Gensou no Grimgar",
character: "Shinohara",
quote: "I understand the pain of losing a comrade."
}
];
因此,您的output()
功能将是这样的:
function output() {
return info.forEach((pack) => {
const { anime, character, quote } = pack; // destructuring
let article = document.createElement('article');
let animeElement = document.createElement("h4");
animeElement.textContent = pack.anime;
let quoteElement = document.createElement("h3");
quoteElement.textContent = pack.quote;
let characterElement = document.createElement("h4");
characterElement.textContent = pack.character;
article.appendChild(animeElement);
article.appendChild(quoteElement);
article.appendChild(characterElement);
let quotes = document.getElementById('quotes');
quotes.appendChild(article);
});
}
window.addEventListener('load', output);
我希望它对你有用!!
编辑:
如果您想通过单击按钮来显示结果,则需要将代码重构如下:
let info = [
{
anime: "Fate/zero",
character: "Rider",
quote: "To win, but not destroy; to conquer, but not humiliate: That is what true conquest is!"
},
{
anime: "Hai to Gensou no Grimgar",
character: "Shinohara",
quote: "I understand the pain of losing a comrade."
}
];
function output() {
info.forEach((pack) => {
const { anime, character, quote } = pack; // destructuring
let article = document.createElement('article');
let animeElement = document.createElement("h4");
animeElement.textContent = pack.anime;
let quoteElement = document.createElement("h3");
quoteElement.textContent = pack.quote;
let characterElement = document.createElement("h4");
characterElement.textContent = pack.character;
article.appendChild(animeElement);
article.appendChild(quoteElement);
article.appendChild(characterElement);
let quotes = document.getElementById('quotes');
quotes.appendChild(article);
});
}
function helper() {
// button
let btn = document.getElementById('btn');
// add event listener to the button which triggered output()
btn.addEventListener('click', output);
}
window.addEventListener('load', helper);
推荐阅读
- machine-learning - 如何获得一个良好的二元分类深度神经模型,其中负数据更多位于数据集上?
- saml - 如何正确关联 Gatling 中的 SAML 标头响应
- android - Android Studio - 找不到 org.jetbrains.kotlin:kotlin-stdlib:1.1.3-2
- sql-server - SQL Server 2014 类型 DATE 不是已定义的系统类型
- javascript - 如何在页面加载时执行的 React 组件中包含 javascript 文件?
- r - 对列中的不同值进行分组并在 R 中创建新列(类似于 SQL 中的 AS)
- python - 在 pandas 中生成具有二进制计数值的交叉表类型数据框
- r - 从 SQL 查询中排除 NULL 值并在数据框中保留旧值
- autodesk-forge - 如何选择指定类型的所有对象,例如 IfcWallStandardCase?
- php - WooCommerce 中的样式 WooCommerce 单产品摘要