<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>JS</title> <style> #iwatch{ width:300px; height:60px; line-height:60px; font-size:40px; text-align:center; border:3px solid #333; background-color:#abcdef; } </style> </head> <body> <h1>表</h1> <hr> <div id="iwatch"> </div> <script> runTime(); //定时调用 setInterval(runTime, 1000); function runTime(){ //获取时分秒 var date = new Date(); var h = date.getHours(); var m = date.getMinutes(); var s = date.getSeconds(); //处理 var h = (h<10)?'0'+h:h; var m = (m<10)?'0'+m:m; var s = (s<10)?'0'+s:s; //拼装字符串 var dateStr = h+' : '+m+' : '+s; //赋值 document.getElementById('iwatch').innerHTML = dateStr; } </script> </body> </html>