<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <title>文本时钟</title> <script type="text/javascript" src="jquery-1.8.3.js"></script> <script type="text/javascript" src="tools.js"></script> <script type="text/javascript"> $(document).ready(function () { setInterval(function () { var dateStr = getDateString(new Date()); $("#div01").html(dateStr); } ,1000); }); </script> </head> <body> <div id="div01"></div> </body> </html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <title>文本时钟练习案例</title> </head> <body> <div id="div01"></div> </body> </html> <script type="text/javascript"> //在页面上以文本形式显示当前时间 yyyy-MM-dd HH:mm:ss , 每秒钟刷新一次 //以yyyy-MM-dd HH:mm:ss这种格式 获得当前时间的日期字符串 function getDateString(){ var now = new Date(); var str = ""; str = str + now.getFullYear(); str += "-"; var month = now.getMonth()+1; //now.getMonth() 0 代表1月 if(month<10){ str += "0"; } str += month; str += "-"; str += now.getDate(); str += " "; str += now.getHours(); str += ":"; str += now.getMinutes(); str += ":"; str += now.getSeconds(); return str; } //1 先把当前日期的文本弄好 //2 把时间文本显示在页面上 //3 每隔一秒刷新一次 setInterval(function(){ var div01 = document.getElementById("div01"); div01.innerHTML = getDateString(); },1000); </script>