日期:2014-05-16  浏览次数:20477 次

初学JavaScript 时钟问题
<html>
<head>
<title>look the status change</title>
</head>
<body>
<script type="text/javascript">
var nowtime ;
var year ;
var month ,date,hours,minutes,seconds,all;
function aaa()
{
nowtime = new Date();
year = nowtime.getYear();
month = nowtime.getMonth()+1;
date = nowtime.getDate();
hours = nowtime.getHours();
minutes = nowtime.getMinutes();
seconds = nowtime.getSeconds();
all = year + " "+month +" "+ date +" "+ hours +":"+minutes+":"+seconds;
document.form.text.value=all;
window.setTimeout("aaa();",1000);
}
aaa();
</script>
</body>
</html>
一直报错 不清楚错在哪里 请高手指教

------解决方案--------------------
<html>
<head>
<title>look the status change</title>
</head>
<body>
<input type="text" name="text" id="text" />
<script type="text/javascript">
var nowtime ;
var year ;
var month ,date,hours,minutes,seconds,all;
function aaa()
{
nowtime = new Date();
year = nowtime.getYear();
month = nowtime.getMonth()+1;
date = nowtime.getDate();
hours = nowtime.getHours();
minutes = nowtime.getMinutes();
seconds = nowtime.getSeconds();
all = year + " "+month +" "+ date +" "+ hours +":"+minutes+":"+seconds;
document.getElementById("text").value=all;
window.setTimeout("aaa();",1000);
}
aaa();
</script>
</body>
</html>
------解决方案--------------------
<html>
<head>
<title>look the status change</title>
<script type="text/javascript">
var nowtime ;
var year ;
var month ,date,hours,minutes,seconds,all;
function aaa()
{
nowtime = new Date();
year = nowtime.getYear();
month = nowtime.getMonth()+1;
date = nowtime.getDate();
hours = nowtime.getHours();
minutes = nowtime.getMinutes();
seconds = nowtime.getSeconds();
all = year + " "+month +" "+ date +" "+ hours +":"+minutes+":"+seconds;
document.myform.text.value=all;
//document.getElementById("txt").value = all;
window.setTimeout("aaa();",1000);
}

</script>
</head>
<body onLoad = "aaa()">
<form name = "myform">

<div><input type = "text" id = "txt" name = "text"></div>
</form>
</body>
</html>

------解决方案--------------------
探讨
再问一个问题 我写成document.write(all) 为什么 时间就跳两次 就报错了 很不理解 谢谢
是不是 在时钟这样的问题上是不能用 document.write()的????

------解决方案--------------------
document.write()输出一次, "window.setTimeout("aaa();",1000);"一秒后又执行一次aaa()方法,故是两次,注意:setTimeout()和setInterval()两者的区别。一般是用innerHTML来显示时间。