js 簡易時鐘
html部分
<div id="clock">
</div>
css部分
#clock{
width:600px ;
text-align: center;
margin:50px auto ;
}
span{
font-size: 32px;
}
js部分
<script type="text/javascript">
function getId(id){ //id傳入的是字符串
return document.getElementById(id);
};
function myDate(){
var myDate=new Date();
var y=myDate.getFullYear();
var m=myDate.getMonth()+1;
var r=myDate.getDate();
var h=myDate.getHours();
var f=myDate.getMinutes();
var s=myDate.getSeconds();
var clockDiv=getId(‘clock‘);
clockDiv.innerHTML="<div><span>"+y+"年</span><span>"+m+"月</span><span>"+r+"日</span></div><div><span>"+h+"時</span><span>"+f+"分</span><span>"+s+"秒</span></div>";
}
window.onload=function(){
setInterval(myDate,1000)
myDate();
}
</script>
js 簡易時鐘