我用javaScript写了一个函数,我在网页中该如何调用它呢?

来源:百度知道 编辑:UC知道 时间:2024/06/06 01:51:52
动态显示时间的函数如下
<script type="text/javascript">
function show()
{
now=new Date();
hours=now.getHours();
minutes=now.getMinutes();
seconds=now.getSeconds();
if(minutes<=9)
minutes="0"+minutes
if(seconds<=9)
seconds="0"+seconds
timer.innerHTML=hours+":"+minutes+":"+seconds;
setTimeout("show()",1000);
}
show();
</script>

<html>
<head>
<title></title>
</head>
<body>
</body>
</html>

<body onload="show()">
<h1 id="id_Value"></h1>
</body>

关键要在body中,加入一个id为"timer"的标签,你的javascrip程序,也只支持IE浏览器,及IE为内核的浏览器,最好用document.getElementById ("id_Value")得到一个Element对象

<script type="text/javascript">
var timer = document.getElementById("id_Value");
function show()
{
now=new Date();
hours=now.getHours();
minutes=now.getMinutes();
seconds=now.getSeconds();
if(minutes<=9)
minutes="0"+minutes
if(seconds<=9)
seconds="0"+seconds
timer.innerHTML=hours+":"+minutes+":"+seconds;
setTimeout("show()",1000);
}
show();
</script>

方法多了.
最简单的 <body onload="show()">

其实如果你把 script 代码写在 <body></body>里面
就直接调用了.

你想在页面哪个地方显示时钟,就在页面那个地方的代码中加<script>show();</s