作者kevintwo (MAI舞)
看板Ajax
標題[問題][js] 一段顯示時間的code
時間Wed Jun 10 00:56:55 2009
以下是code部份
不知為什麼 跑不出結果
用 alert(timer)去看會顯示 null(在firefox下)
為什麼呢??????? orz//////
(function() {
var self = this;
var now,hours,minutes,seconds,timeValue;
var timer = document.getElementById('tmr');
this.init = function() {
setInterval("self.update()", 1);
};
this.update = function() {
now = new Date();
hours = now.getHours();
minutes = now.getMinutes();
seconds = now.getSeconds();
timeValue = (hours >= 12) ? "下午 " : "上午 ";
timeValue += ((hours > 12) ? hours - 12 : hours) + " 點";
timeValue += ((minutes < 10) ? " 0" : " ") + minutes + " 分";
timeValue += ((seconds < 10) ? " 0" : " ") + seconds + " 秒";
timer.value = timeValue;
};
this.init();
})();
document.write('<div id="righttime"><input type="text" id="tmr" style="
width: 190px; border-width:0px; margin: 0 auto; font-size: 20px;
background-color: blue; vertical-align: middle; color: white;" /></div>');
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.113.95.110
1F:→ kevintwo:發現把 getElementById 那行,放到 update function 06/10 01:01
2F:→ kevintwo:就可以正常,但還是不知為什麼? 06/10 01:01
※ 編輯: kevintwo 來自: 140.113.95.110 (06/10 01:02)
※ 編輯: kevintwo 來自: 140.113.95.110 (06/10 01:03)
3F:→ fillano:這是邏輯問題,你要把document.write移到前面才行。 06/10 11:05
4F:→ kevintwo:對喔~還沒讀到那~謝謝大大提醒^^a 連這個都注意不到orz// 06/10 19:33