<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
<TITLE>显示时间</TITLE>
<SCRIPT language="javascript">
function show_time()
{
    var d = new Date();
    var h = d.getHours();
    var m = d.getMinutes();
    var s = d.getSeconds();

    if(h == 0) h=12;
    if(h<10) h="0"+h;
    if(m<10) m="0"+m;
    if(s<10) s="0"+s;
    var t = h+":"+m+":"+s;
    timeTd.innerText = t;
    setTimeout("show_time()",1000);
}
</SCRIPT>
</HEAD><BODY onLoad="show_time()">
<table>
<tr>
<td id=timeTd></td>
</tr>
</table>
</BODY>
</HTML>

解决方案 »

  1.   

    要定时的话,就加个定时器,定时执行"showtime".
    window.setInterval()具体用法你自己查
      

  2.   

    to LxcJie(肖冲):为什么我添加了年月日后,运行时提示说页面有错误呢?我这个函数对吗?<HTML>
    <HEAD>
    <META http-equiv="Content-Type" content="text/html; charset=gb2312">
    <TITLE>显示时间</TITLE>
    <SCRIPT language="javascript">
    function show_time()
    {
    date=new Date();
    year=date.getYear();
    month=date.getMonth()+1;
    day=date.getDate();
    hour=date.getHours();
    minute=date.getMinutes();
    second=date.getSeconds();

    myArray=new Array(6);
    myArray[0]="星期日"
    myArray[1]="星期一"
    myArray[2]="星期二"
    myArray[3]="星期三"
    myArray[4]="星期四"
    myArray[5]="星期五"
    myArray[6]="星期六"
    weekday=date.getDay();

    if(hour == 0)hour=12;
    if(hour<10) hour="0"+hour;
    if(minute<10) minute="0"+minute;
    if(seconds<10) second="0"+second;
    timeString=year+"年"+month+"月"+day+"日&nbsp;&nbsp"+myArray[weekday]
    +"&nbsp;&nbsp;"+hour+":"+minute+":"+second;
    timeTd.innerText=timeString;
    setTimeout("show_time()",1000);
    }
    </SCRIPT>
    </HEAD><BODY onLoad="show_time()">
    <table>
    <tr>
    <td id=timeTd></td>
    </tr>
    </table>
    </BODY>
    </HTML>
      

  3.   

    嗯,对,但是if(seconds<10) second="0"+second;写错了if(seconds<10) second="0"+second;--》if(second<10) second="0"+second;