javascript 如何使当前时间出现在警告框中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17469958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make the current time appear in an alert box
提问by Artemis F
How does one make the current time appear in an alert box? I'm new to programming, so I'm completely lost. I'm coding in html5 or javascript. I've added most of my code here.
如何使当前时间出现在警告框中?我是编程新手,所以我完全迷失了。我正在用 html5 或 javascript 编码。我在这里添加了大部分代码。
Here's my code:
这是我的代码:
<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>
Am I doing something wrong?
难道我做错了什么?
回答by Bill Criswell
First, your HTML is completely screwed. Validate it.
首先,您的 HTML 完全被搞砸了。验证它。
As for the code itself, startTime
is broken. It needs new Date()
.
至于代码本身,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(':')
}
and to get it into an alert box you can just do:
并将其放入警报框中,您可以这样做:
alert(startTime());
If you want to put it in an element like <div id="txt"></div>
you can do:
如果你想把它放在一个元素中,<div id="txt"></div>
你可以这样做:
document.getElementById('txt').innerHTML = startTime();
回答by mohkhan
You can do something like this.
你可以做这样的事情。
...
else if(x.value == "current time"){
startTime();
alert("The current time is: " + document.getElementById('txt').innerHTML);
...
回答by Bergi
Make a function getTime
that returns the current time string:
创建一个getTime
返回当前时间字符串的函数:
function getTime(){
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
return h+":"+m+":"+s;
}
Then use that:
然后使用它:
…
else if(x.value == "current time"){
alert("The current time is: " + getTime());
}
…
If you still need the startTime
function, you can use it there as well:
如果您仍然需要该startTime
功能,您也可以在那里使用它:
function startTime() {
document.getElementById('txt').innerHTML = getTime();
}