【发布时间】:2021-05-06 11:52:02
【问题描述】:
我正在尝试用 Javascript 重新制作我用 Python 制作的文字游戏。我在做一些基本的事情时遇到了麻烦。
脚本应该加载一个单词列表,当您单击按钮时,它应该显示列表中的一个随机单词。我尝试了几种方法,但在再次单击按钮时显示的索引号并没有刷新。
所以我在这个阶段的问题是:如何通过单击选择随机数组项,然后将其删除,以便再次单击时不会再次出现。我确实想在之后添加显示单词和 TTS 的时间限制,但这将在稍后进行。
<html>
<head>
<title>Aliyah's dictee spel</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="header">
<h1>Hej! Velkommen til Aliyahs diktee!</h1>
</div>
<div id="Random_word">
<h2 id="Empty">Click new word to start</h2>
<button id="refresh_word">new word</button>
</div>
<input type="text" id="input1" />
<input type="submit" name="ok" value="ok"/>
<script>
var words = ["test1", "test2", "test3"];
var wordsCorrect = 0;
var wordsWrong = 0;
newWord(words)
{
var random = Math.floor(Math.random() * words.length);
document.getElementById("Empty").innerHTML = random;
};
document.getElementById("refresh_word").addEventListener("click", newWord());
</script>
</body>
</html>
【问题讨论】:
-
()在函数引用后面总是立即调用该函数。
标签: javascript arrays random