网页上动态显示时间?
<html>
<head>
<title> 小程序 </title>
</head>
<body>
<script language= "JavaScript ">
function showTime()
{
var thetime= new Date(); // 初始化日期对象
document.write(thetime.toLocaleString());
window.setTimeout( "showTime() ",1000);
}
window.setTimeout( "showTime() ",1000);
</script>
</body>
</html>
我想在网页上动态地显示时间,上面这段怎么不行呢?说是缺少对象,请问缺少什么对象?
------解决方案-------------------- <html>
<head>
<title> 小程序 </title>
</head>
<body>
<script language= "JavaScript ">
function showTime()
{
var thetime= new Date(); // 初始化日期对象
document.write(thetime.toLocaleString());
alert(document.documentElement.outerHTML);
window.setTimeout( "alert(showTime);showTime() ",1000);
}
window.setTimeout( "showTime() ",1000);
</script>
</body>
</html>
要慎用document.write方法,因为当文档载入完毕之后,调用此方法会清除原文档中的内容,
所以上述错误是由于清除了showTime方法导致,找不到了showTime方法。
------解决方案-------------------- <html>
<head>
<title> 小程序 </title>
</head>
<body>
<script language= "JavaScript ">
function showTime()
{
var thetime= new Date(); // 初始化日期对象
document.getElementById( "timeArea ").innerText = thetime.toLocaleString();
window.setTimeout( "showTime() ",1000);
}
window.setTimeout( "showTime() ",1000);
</script>
<div id= "timeArea "> </div>
</body>
</html>
建议采用修改页面某个dom节点的innerText方式实现
------解决方案-------------------- <html>
<head>
<title> 小程序 </title>
</head>
<body>
<div id=divtime> </div>
<script language= "JavaScript ">
window.onload=showTime();
function showTime()
{
var thetime= new Date(); // 初始化日期对象
divtime.innerHTML=thetime.toLocaleString();
window.setTimeout( "showTime() ",1000);
}
</script>
</body>
</html>