对于我正在制作的一个小游戏,当在输入字段中给出正确答案时,需要将数组列表一个一个地放入一个 html 元素中。当答案错误时,需要用透明颜色打印该单词。
我已经尝试过使用 for 循环,但这会一次输出所有字符串,而不是一个一个地输出。我读到我应该使用++,但不知道如何使用。
它应该按照正确的顺序逐一打印单词(这样句子就变得可读)。该代码目前没有给出任何错误,但没有给出所需的结果。
form.onsubmit = (e) => {
var essay = ["Slow", "Reading - ", "Carolyn", "Strauss", "By", "definition,", "‘reading’", "goes", "well", "beyond", "the", "grasping", "of", "written", "characters", "and", "the", "realm", "of", "ideas", "they", "express.", "Applied", "to", "environments,", "systems,", "and", "relationships,", "‘reading’"];
var tekst = "";
var i;
for (i = 0; i < essay.length; i++) {
tekst += essay[i];
}
if (input.value === text.style.color) {
goodAnswers++;
document.getElementById("essaywords").innerHTML = essay[i];
}
// here you do what ever supposed to happen after a good answer
else {
badAnswers++;
document.getElementById("essaywords").innerHTML = "False!";
// here you do what ever supposed to happen after a wrong answer
setColor();
}
最佳答案
也许你的意思是这样的?
无论如何,这都是一个开始
var essay = ["Slow", "Reading - ", "Carolyn", "Strauss", "By", "definition,", "‘reading’", "goes", "well", "beyond", "the", "grasping", "of", "written", "characters", "and", "the", "realm", "of", "ideas", "they", "express.", "Applied", "to", "environments,", "systems,", "and", "relationships,", "‘reading’"];
var cnt = 0,
goodAnswers = 0,
badAnswers = 0;
document.getElementById("form").addEventListener("submit", (e) => {
if (cnt >= essay.length) return;
e.preventDefault(); // stop actual submission
if (document.getElementById("textInput").value === "yes") { // I do not know what you mean by text.style.color
goodAnswers++;
document.getElementById("essaywords").innerHTML += essay[cnt]+" ";
} else {
badAnswers++;
document.getElementById("essaywords").innerHTML += '<span style="color:white">' + essay[cnt] + ' </span>'
}
cnt++
})
<form id="form">
<input type="text" id="textInput" value="" />
<input type="submit" value="GO" />
</form>
<div id="essaywords"></div>
关于javascript - 如何制作一个js数组列表,一一打印出来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56562198/