如何让 innerHTML 显示响应中的多个对象?

问题描述 投票:0回答:1

所以我有这个函数,它从包含“字符”和“引号”的 Json 文件中获取随机数组。我怎样才能在这里得到这个函数来显示两个对象,而不像我的代码当前正在做的那样调用函数两次?

const kaneki = document.querySelector("#kaneki-button");
if (kaneki) {
    kaneki.addEventListener("click", function () {
        fetch("json/ghoul.json")
            .then((response) => response.json())
            .then((response) => {
                document.getElementById("quote").innerHTML = quoteGenerator(response)["quote"];
                document.getElementById("character").innerHTML =  quoteGenerator(response)["character"];
            });
    });
}

javascript html response innerhtml getelementbyid
1个回答
0
投票

我假设你指的是“功能”

quoteGenerator
。你能像这样保存
quoteGenerator
的结果吗?

const kaneki = document.querySelector("#kaneki-button");
if (kaneki) {
    kaneki.addEventListener("click", function () {
        fetch("json/ghoul.json")
            .then((response) => response.json())
            .then((response) => {
                let x = quoteGenerator(response);
                document.getElementById("quote").innerHTML = x["quote"];
                document.getElementById("character").innerHTML =  x["character"];
            });
    });
}
© www.soinside.com 2019 - 2024. All rights reserved.