<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>
<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>
解决方案 »
- Infinite Scroll的使用问题
- 封装YUI DOC的JS文档化工具
- overflow:hidden 感觉没有多少用啊,谈谈他的存在性
- 【JS】一种简洁实用的对象设计模型
- 高问题 请进
- 链接一个窗口 这个穿口的地址栏 菜单栏都不要 应该怎么做啊
- 英语单词或句子即时发音的功能
- 怎么判断复选框已经选上或没有选上,为什么我判断时总是显示1(已选)
- 如图 跪求各位大神这样的要怎样写
- jquery 单个个each函数叠加遍历使用的使用出现多次重复,怎样消除?
- 怎么样删除数组中的重复元素?大家来看看我的算法。欢迎斧正。
- (在线等)如何获取execcommand('insertimage',true,'')中得到的图片路径呀?
<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>
第一:在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>