HTML的动态时间如何做…

html-css020

HTML的动态时间如何做…,第1张

纯HTML不可能实现,可以借助于JAVASCRIPT的 setInterval或setTimeout来定时执行一个更新文本的函数来实现。给你一段代码:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns=" http://www.w3.org/1999/xhtml">

<head>

<title>new document </title>

<meta name="generator" content="editplus" />

<meta name="author" content="" />

<meta name="keywords" content="" />

<meta name="description" content="" />

<script type="text/javascript">

<!--

setInterval("showTime()",1000)

function showTime()

{

var wk = ["星期日","星期一","星期二","星期三","星期四","星期五","星期六"]

var date = new Date()

var msg = "今天是:" + date.getFullYear()+"年"

msg += date.getMonth()+1 +"月"

msg += date.getDate() + "日"

msg += wk[date.getDay()]+" "

msg += date.getHours()+":" + date.getMinutes()+":"+date.getSeconds()

document.getElementById("time").innerText = msg

}

//-->

</script>

</head>

<body>

<span id="time"></span>

</body>

</html>

<script>

window.onload=function(){

 function toer(n){

    if(n<10){

       return "0"+n

    }

    else{

       return ""+n

    }

 }

 setInterval(function(){

    var nowtime=document.getElementById("nowtime")

    var odate=new Date()

    var aa=toer(odate.getHours())+": "+toer(odate.getMinutes())+": "+toer(odate.getSeconds())

    nowtime.innerHTML = aa

 },1000)

}

</script>