gpt4 book ai didi

javascript - 如何使当前时间出现在警告框中

转载 作者:行者123 更新时间:2023-11-30 17:59:06 25 4
gpt4 key购买 nike

如何使当前时间出现在警告框中?我是编程新手,所以我完全迷路了。我正在用 html5 或 javascript 编码。我已经在此处添加了大部分代码。

这是我的代码:

<script>
function startTime(){
var today=Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
document.getElementById('txt').innerHTML=h+":"+m+":"+s;
}

</script>
<div id="txt"></div>

<h1>What would you like it to say?</h1>

<p>Commmon Requests:</p>

<form action="">
<select name="requests" onchange ="checkIfOther();" id="dropDown1">
<option value="blank"> </option>
<option value="good morning sir">Good Morning Sir</option>
<option value="current time">Current Time</option>
<option value="other">Other</option>
</select>
</form>

</p>
<button onclick="mySubmit()" value>Submit</button>

<div id="other" style="display:none">
<br><br><label>Optional Request: </label><input type="text" id="otherText"/>
</div>

</body>
</html>

<script>
function checkIfOther(){
a=document.getElementById("dropDown1");
if(a.value == "other"){
document.getElementById("other").setAttribute("style","display:inline");
}
else{
document.getElementById("other").setAttribute("style","display:none");
}
}
</script>

<script type="text/javascript">
function mySubmit(){
var x=document.getElementById("dropDown1");
if(x.value == "other"){
alert("You have chosen: " + otherText.value);
}
else if(x.value == "current time"){
alert("The current time is: " + 'txt'.value); //what do I do here?
}
else{
alert("You have chosen: " + x.options[x.selectedIndex].text);
}
}
</script>

我做错了什么吗?

最佳答案

首先,您的 HTML 完全搞砸了。验证它。

至于代码本身,startTime被打破。它需要 new Date() .

function startTime(){
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
return [ h, m, s ].join(':')
}

要将其放入警告框,您只需执行以下操作:

alert(startTime());

如果你想把它放在像<div id="txt"></div> 这样的元素中你可以这样做:

document.getElementById('txt').innerHTML = startTime();

关于javascript - 如何使当前时间出现在警告框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17469958/

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