这段简单的javacsript程序哪里错了?

来源:百度知道 编辑:UC知道 时间:2024/05/05 14:38:41
一个关于时间的小程序,但却没有出现预期效果,请问哪里错了?(可能包括了拼写错误)
<body>
<script language="javascript" type="text/javascript">
<!--
var theDate=new Date();
var hours=theDate.getHours();
var mins=theDate.getMinutes();
var sec=theDate.getSeconds();
function thetime()
{
document.write(hours+":"+mins+":"+sec);
setTimeout("thetime()",1000);
}
thetime();
//-->
</script>
</body>

document.write会把所有的页面内容都覆盖掉,你的程序就不存在了
写一个div然后
document.getElementById("DivId").innerHTML ="xxx"

变量的位置也没放对,正确的程序:

<body>
<div id="time">
</div>
<script language="javascript" type="text/javascript">
<!--
function thetime()
{
var theDate=new Date();
var hours=theDate.getHours();
var mins=theDate.getMinutes();
var sec=theDate.getSeconds();
document.getElementById("time").innerHTML =hours+":"+mins+":"+sec;
setTimeout("thetime()",1000);
}
thetime();
//-->
</script>
</body>

改成这样

<div id="nowtime"></div>
<script language="javascript" type="text/javascript">
<!--
function thetime()
{
var theDate=new Date();
var hours=theDate.getHo