需要大家的帮助,一个显示时间的问题!

tonychi23 2005-03-07 06:47:58
我要做一个文本框,然后获取时间,按上下午的表示方式显示,可是总有错误,大家帮我看看!代码如下:<html>
<title>显示时间</title>
<head>
<script language="javascript">
var time=new Date();
with(time)
{
h=getHours();
if(h<=12)
{document.myform.text1.value="h+":"+getMinutes()+":"+getSeconds()+"AM""};
if(h>12&&h<=24)
{h=24-getHours();
document.myform.text1.value="h+":"+getMinutes()+":"+getSeconds()+"PM""};
if(h>24)
(alert("请输入正确的时间!"));
}

</script>
</head>
<body>
<form name=myform>
<b>现在时间为:</b>
<input name="text1" value="">
</body>
</html>
...全文
95 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
tonychi23 2005-03-08
  • 打赏
  • 举报
回复
大家还有什么想指导的么?
尽管说说,谢谢!
tonychi23 2005-03-07
  • 打赏
  • 举报
回复
谈几点感受:
第一:在getHours()的时候,因为它的值不可能大于23,所以没有必要if(h>23)的情况;
第二:在计算的时候注意:h=24-getHours();
第三:在输出的时候,value的值,可以直接写,不用""号;
有一点不解:
为什么将<script>... ...</script>放在<head>中就显示错误呢?
附:最终代码<html>
<title>显示时间</title>
<head>

</head>
<body>
<form name="myform">
<b>现在时间为:</b>
<input name="text1" value="">
<script language="javascript">
var time=new Date();
with(time)
{
h=getHours();
if(h<=12)
{
document.myform.text1.value=h+":"+getMinutes()+":"+getSeconds()+"AM";
}
if(h>12)
{
h=getHours()-12;
document.myform.text1.value=h+":"+getMinutes()+":"+getSeconds()+"PM";
}
}

</script>
</body>

</html>

suhongwei 2005-03-07
  • 打赏
  • 举报
回复
<html>
<title>显示时间</title>
<head>

</head>
<body>
<form name="myform">
<b>现在时间为:</b>
<input name="text1" value="">
</body>
<script language="javascript">
var time=new Date();
with(time)
{
h=getHours();
if(h<=12)
{
document.myform.text1.value=h+":"+getMinutes()+":"+getSeconds()+"AM";
}
if(h>12&&h<24)
{
h=24-getHours();
document.myform.text1.value=h+":"+getMinutes()+":"+getSeconds()+"PM";
}
}

</script>
</html>
vivianfdlpw 2005-03-07
  • 打赏
  • 举报
回复
<html>
<title>显示时间</title>
<head>

</head>
<body>
<form name=myform>
<b>现在时间为:</b>
<input name="text1" value="">
</body>
<script language="javascript">
var time=new Date();
with(time)
{
h=getHours();
if(h<=12)
{document.myform.text1.value="h"+":"+getMinutes()+":"+getSeconds()+"AM"};
if(h>12&&h<=24)
{h=24-getHours();
document.myform.text1.value="h"+":"+getMinutes()+":"+getSeconds()+"PM"};
if(h>24)
(alert("请输入正确的时间!"));
}

</script>
</html>

87,992

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧