gpt4 book ai didi

javascript - 当我在输入中询问时,如何让 Javascript 显示当前时间?

转载 作者:行者123 更新时间:2023-11-28 19:11:13 26 4
gpt4 key购买 nike

我想查看在输入中输入某些特定文本时的当前时间,但它不起作用

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com