實(shí)時(shí)鐘表在body里加載不了,非要點(diǎn)擊按鈕才能實(shí)現(xiàn)
本來想在<body onload="simpleWatch()">實(shí)現(xiàn)網(wǎng)頁加載時(shí)顯示實(shí)時(shí)鐘表,但是顯示不了,非的用按鈕點(diǎn)擊?
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>一個(gè)簡單的鐘表</title>
<script type="text/javascript">
function simpleWatch(){
var date=new Date();
var year=date.getFullYear();
var month=date.getMonth();
month=addZero(month);
var day=date.getDate();
day=addZero(day);
var hour=date.getHours();
hour=addZero(hour);
var minute=date.getMinutes();
minute=addZero(minute);
var second=date.getSeconds();
second=addZero(second);
document.getElementById("p1").innerHTML=
year+"年"+month+"月"+day+"日"+hour+":"+minute+":"+second;
setTimeout("simpleWatch()",1000);
}
function addZero(x){
if(x<10){
x="0"+x;
}
return x;
}
</script>
</head>
<body>
<div id="p1"></div>
<input type="button" value="點(diǎn)擊" onclick="simpleWatch()"/>
</body>
</html>
2018-11-14
?var s = setInterval(function () {
? ? ? ? ?var date = new Date();
? ? ?document.getElementById("p1").innerHTML = date;
? ? ?},1000);
//這樣就是實(shí)時(shí)了