我想查看在输入中输入某些特定文本时的当前时间,但它不起作用
HTML
<form>
<input type="text" id="inputcommand"></input>
<input type="button" value="Submit" onclick="showTime()"></input>
</form>
<p id="output">Output here</p>
Javascript
var command = document.getElementById("inputcommand");
var output = document.getElementById("output");
var showTime = function(){
var askForTime = ["What time is it?","Show me time","Time"];
if(command.value === askForTime){
output.innerHTML = Date();
}
};
最佳答案
使用for循环迭代数组值askForTime
var command = document.getElementById("inputcommand");
var output = document.getElementById("output");
var showTime = function(){
var askForTime = ["What time is it?","Show me time","Time"];
for(var i=0; i<askForTime.length; i++)
if(command.value == askForTime[i]){
output.innerHTML = Date();
}
};
关于javascript - 当我在输入中询问时,如何让 Javascript 显示当前时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30693140/