[問題][js] 一段顯示時間的code

看板Ajax作者 (MAI舞)時間16年前 (2009/06/10 00:56), 編輯推噓0(004)
留言4則, 2人參與, 最新討論串1/1
以下是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

06/10 01:01, , 1F
發現把 getElementById 那行,放到 update function
06/10 01:01, 1F

06/10 01:01, , 2F
就可以正常,但還是不知為什麼?
06/10 01:01, 2F
※ 編輯: kevintwo 來自: 140.113.95.110 (06/10 01:02) ※ 編輯: kevintwo 來自: 140.113.95.110 (06/10 01:03)

06/10 11:05, , 3F
這是邏輯問題,你要把document.write移到前面才行。
06/10 11:05, 3F

06/10 19:33, , 4F
對喔~還沒讀到那~謝謝大大提醒^^a 連這個都注意不到orz//
06/10 19:33, 4F
文章代碼(AID): #1ABfHOvD (Ajax)
文章代碼(AID): #1ABfHOvD (Ajax)