js动态数字时钟代码请教

JavaScript026

js动态数字时钟代码请教,第1张

_time.getHours() + 100

这里得到的数字

+ ""

是把这个数字转换成字符

才能执行

substr

否则会出错

因为js代码是从上到下执行的,所以

var clock = document.getElementById("myclock")

找不到元素

clock是空的

所以可以将js代码放到div后面,也可以将 var clock = document.getElementById("myclock")

定义放到 setInterval的function里

如:

<script type="text/javascript" language="javascript">

setInterval(function (){

var clock = document.getElementById("myclock")

var d = new Date()

clock.innerHTML = d.getHours() + ":" + d.getMinutes() + ":" +d.getSeconds()

},1000)

</script>

<div id="myclock">

00:00:00

</div>

<html>

<head>

<script language="JavaScript">

function showTime()

{

var currentDate = new Date()

var hours = currentDate.getHours()

var minutes = currentDate.getMinutes()

var seconds = currentDate.getSeconds()

//

//格式化输出

//

if (minutes <10)

minutes = "0" + minutes

if (seconds <10)

seconds = "0" + seconds

var currentTimeString ="<font color=blue>" + hours + ":" + minutes + ":" + seconds + "</font>"

document.getElementById("show").innerHTML=currentTimeString //改这地方

window.setTimeout("showTime()", 1000)

}

</script>

</head>

<body onload="showTime()">

<span id="show"></span> <!--加这地方-->

</body>

</html>