我正在爲我妻子的一年級開發移動應用程序,以便他們可以練習視覺單詞。我是JavaScript的新手,但我能夠實現我的第一個目標,即獲取JavaScript數組並從中提取一個隨機單詞。我的第二個目標是讓用戶鍵入他們看到的單詞,單擊一個按鈕並將他們輸入的單詞與隨機單詞進行比較。我試圖用第二個函數做這件事,但它沒有做到這一點。我沒有在控制檯中發現任何錯誤,所以我在如何使這個工作方面有點失落。任何幫助將非常感謝我和一大羣一年級學生。這是我迄今爲止的代碼。在JavaScript中比較兩個函數
<!DOCTYPE html>
<html>
<body>
<h1>Practice Spelling Test</h1>
<p id="aWord"></p>
<input id="yourTurn">
<button onclick="myFunction()">New Word</button>
<button onclick="checkSpelling()">Check My Spelling</button>
<p id="result"></p>
<script>
var sightWord = ["hoof", "hook", "shook", "hood", "wood", "took", "book", "good", "food", "mood", "look"];
var yourTurn = document.getElementById("yourTurn").value;
var aWord = document.getElementById("aWord").value;
var checkWord = (yourTurn == aWord)?"Nice Job!":"So close! Try again!";
function myFunction() {
var showWord = sightWord[Math.floor((Math.random()*10)+1)];
aWord.innerHTML = showWord;
}
function checkSpelling(result) {
document.getElementById("result").innerHTML=checkWord;
}
</script>
</body>
</html>
謝謝!我非常感謝幫助和鏈接到jsbin,以便在行動中看到它! – 2013-04-08 20:42:43