HTML Code:
<div id="clock"></div>
JS Code:
function changeClock() { var d = new Date(); var mm = (d.getMinutes() < 10) ? ('0' + d.getMinutes()):d.getMinutes(); //条件运算符给小于10的时间前补0 var ss = (d.getSeconds() < 10) ? ('0' + d.getSeconds()):d.getSeconds(); document.getElementById("clock").innerHTML = d.getHours() + ":" + mm + ":" + ss; }
window.setInterval(changeClock, 1000);