作者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