用于下一个输出的明文
JS Clear text for next output
下面的代码打乱了一个短语,然后要求用户重新排列成正确的逻辑顺序,当输入正确的短语时,代码输出"正确答案",然后输出第二个问题,但是输出时,"正确答案"仍然显示。我怎样才能为下一题做准备呢?
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>
添加:
document.getElementById('feedback').text = '';
在您想要清除"正确答案"的地方。
你可以将它绑定到文本框上的onKeyUp事件监听器。这样,当用户开始输入下一个答案时,它将清除"正确答案"。
您可以像这样为输入字段附加一个事件侦听器
<input onfocus='document.getElementById("feedback").innerHTML = "";'>
这将在每次输入集中时清除反馈
这将在3秒后隐藏两个消息。
if (document.myForm.textinput.value == correctInput[currentQuestion]) {
elMsg.textContent= "right answer";
currentQuestion++;
showQuestion(currentQuestion);
} else {
elMsg.textContent= "wrong answer";
}
setTimeout((function (elMsg) { return function () {
elMsg.textContent = '';
}})(elMsg), 3000)
相关文章:
- 在JavaScript中输出转义字符
- 如何在jQuery中将函数的输出分配给变量
- Javascript,输出结果后页面不断刷新
- Datetime格式为Friendly Time.Moment JS输出错误
- 如何将angularjs中的javascript字符串输出为循环数组
- HTML5FileReader输出到D3.js图表
- console.log以外的Javascript输出函数
- Eloquent JavaScript递归示例如何终止为返回1,但仍然输出指数值
- PHP中的数组输出
- 以不同的顺序输出数据
- 使用Rivets.js输出类
- 在Chrome扩展内部输出Google API调用
- 如何为d3.js图表输出组织/嵌套数据
- 将IndexedDB中的数据拉入数组,并通过ReactJS输出
- 一个ajax循环有两个输出错误innerHTML
- 快速排序程序未正确输出
- 函数将输出nan而不是数字,为什么
- 在量角器中创建一个.txt错误输出文件是手动的,而不是控制台错误
- 如何使用javascript/jQuery将数组的内容输出为明文
- 用于下一个输出的明文