<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>

解决方案 »

  1.   

    <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>
      

  2.   

    谈几点感受:
    第一:在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>