下面的代碼對一個短語進行粗加,然後要求用戶重新排列成正確的邏輯順序,當輸入正確的短語時,代碼輸出'正確答案',然後輸出第二個問題,但是與輸出,'正確答案'仍然顯示。如何爲下一個問題清除它?JS下一個輸出的明文
帕烏THX
var currentQuestion = 0;
var words =
[
['how', 'are', 'you', 'today?'],
['what', 'would', 'you', 'like', 'for', 'breakfast?'],
['what', 'would', 'you', 'like', 'for', 'tea?']
];
var correctInput = [
['how are you today?'],
['what would you like for breakfast?'],
['what would you like for tea?']
];
function showQuestion(i) {
if(i < words.length) {
document.myForm.textinput.value = '';
newWords = words[i].slice(0);
shuffle(newWords);
var el = document.getElementById('phrase');
el.textContent = newWords.join(' ');
}
}
function setup() {
showQuestion(currentQuestion);
var form = document.getElementById('myform');
form.addEventListener('submit', checkAnswer, false);
}
function checkAnswer(e){
e.preventDefault();
var elMsg = document.getElementById('feedback');
if (document.myForm.textinput.value == correctInput[currentQuestion]) {
elMsg.textContent= "right answer";
currentQuestion++;
showQuestion(currentQuestion);
} else {
elMsg.textContent= "wrong answer";
}
}
function shuffle(newWords) {
var counter = newWords.length, temp, index;
while (counter > 0) {
index = Math.floor(Math.random() * counter);
counter--;
temp = newWords[counter];
newWords[counter] = newWords[index];
newWords[index] = temp;}
return newWords;}
setup();
<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
</head>
<body>
<form name="myForm" id ="myform">
<div id ="phrase"></div>
<input type = "text" id = "textinput">
<button id="myBtn">Click here</button>
<div id ="feedback"></div>
</form>
<script src = "phraseScrambler.js"></script>
</body>
</html>
適用於Chrome瀏覽器。什麼不適合你? – mplungjan