我正試圖在<p></p>
標籤中包含一個實時時鐘。在html中顯示javascript日期
我的js代碼如下:
function updateClock ()
{
var currentTime = new Date ();
var currentHours = currentTime.getHours ();
var currentMinutes = currentTime.getMinutes ();
var currentSeconds = currentTime.getSeconds ();
// Pad the minutes and seconds with leading zeros, if required
currentMinutes = (currentMinutes < 10 ? "0" : "") + currentMinutes;
currentSeconds = (currentSeconds < 10 ? "0" : "") + currentSeconds;
// Choose either "AM" or "PM" as appropriate
var timeOfDay = (currentHours < 12) ? "AM" : "PM";
// Convert the hours component to 12-hour format if needed
currentHours = (currentHours > 12) ? currentHours - 12 : currentHours;
// Convert an hours component of "0" to "12"
currentHours = (currentHours == 0) ? 12 : currentHours;
// Compose the string for display
var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;
// Update the time display
document.getElementById("clock").firstChild.nodeValue = currentTimeString;
}
這是在 「date.js」 文件,該文件我已經包含在HTML代碼
的HTML代碼
<p id="clock"> </p>
但沒有顯示正確的... 任何解決方案?
你的意思是,你複製了一些代碼,並沒有立即開始工作,現在想讓我們爲你調試?我想如果你告訴我們你試過的東西,你得到的是什麼錯誤信息等等,你會更好。 –
你甚至在任何地方調用函數'updateClock()'嗎? –
http://jsfiddle.net/QvuES/至少它可以在Google Chrome和IE9上運行。 1.使用控制檯檢查錯誤2.將'firstChild.nodeValue'改爲'innerText' – JiminP