我正在拔头发:我的Javascript时钟无效。我正在使用Firebug来查找错误,但我没有得到任何输出。
图片文件位于子文件夹Dual_Months
和Dual_Numbers
中。我在我的函数中添加了document.write();
以查看它们是否正在触发,但我没有得到任何响应。如果有人发现任何明显错误,我将非常感谢您的帮助。
var imageclock=new Object()
imageclock.digits=["num_0.png", "num_1.png", "num_2.png", "num_3.png", "num_4.png", "num_5.png", "num_6.png", "num_7.png", "num_8.png", "num_9.png", "am.png", "pm.png", "colon.png"]
imageclock.months=["dual_jan.png", "dual_feb.png", "dual_mar.png", "dual_apr.png", "dual_may.png", "dual_jun.png", "dual_jul.png", "dual_aug.png", "dual_sept.png", "dual_oct.png", "dual_nov.png", "dual_dec.png"]
imageclock.instances=0
var preloadimages=[]
var preloadmonths=[]
for (var i=0; i<imageclock.months.length; i++){ //preload images
preloadmonths[i]=new Image()
preloadmonths[i].src="Dual_Months"+imageclock.months[i]
}
for (var i=0; i<imageclock.digits.length; i++){ //preload images
preloadimages[i]=new Image()
preloadimages[i].src="Dual_Numbers"+imageclock.digits[i]
}
imageclock.imageHTML=function(timestring){
//return timestring (ie: 1:56:38) into string of images instead
var sections=timestring.split("/")
for (var i=0; i<sections.length; i++){
if (sections[i] == sections[0])
sections[i]='<img src="Dual_Months/'+imageclock.months[0]+'" />'
else if(sections[i].length==1 && sections[i] == sections[1])
sections[i]='<img src="Dual_Numbers/'+imageclock.digits[0]'" />'+'<img src="Dual_Numbers/'+imageclock.digits[parseInt(sections[i])]+'" />'
else if(sections[i].length!=1 && sections[i] != sections[0])
sections[i]='<img src="Dual_Numbers/'+imageclock.digits[parseInt(sections[i].charAt(0))]+'" />'+'<img src="Dual_Numbers/'+imageclock.digits[parseInt(sections[i].charAt(1))]+'" />'
}
return sections[0]+'<img src="single_nums/'+imageclock.digits[12]+'" />'+sections[1]+'<img src="single_nums/'+imageclock.digits[12]+'" />'+sections[2]
}
imageclock.display=function(){
var clockinstance=this
this.spanid="clockspan"+(imageclock.instances++)
document.write('<span id="'+this.spanid+'"></span>')
this.update()
setInterval(function(){clockinstance.update()}, 1000)
}
imageclock.display.prototype.update=function(){
var dateobj=new Date()
var currentdate=dateobj.getMonth()+"/"+dateobj.getDay()
document.write(currentdate)
var currenttimeHTML=imageclock.imageHTML(currentdate)
document.getElementById(this.spanid).innerHTML=currenttimeHTML
}
new imageclock.display();
答案 0 :(得分:2)
您的脚本中存在一些语法错误。
在此行末尾缺少'
:
sections[i]='<img src="Dual_Months/'+imageclock.months[0]+'" />
sections[i]='<img src="Dual_Months/'+imageclock.months[0]+'" />'
在此行中缺少+
:
sections[i]='<img src="Dual_Numbers/'+imageclock.digits[0]'" />'+'<img src="Dual_Numbers/'+imageclock.digits[parseInt(sections[i])]+'" />'
sections[i]='<img src="Dual_Numbers/'+imageclock.digits[0]+'" />'+'<img src="Dual_Numbers/'+imageclock.digits[parseInt(sections[i])]+'" />'
答案 1 :(得分:2)
你的javascript语法错误首先纠正
if (sections[i] == sections[0])
sections[i]='<img src="Dual_Months/'+imageclock.months[0]+'" />'
else if(sections[i].length==1 && sections[i] == sections[1])
sections[i]='<img src="Dual_Numbers/'+imageclock.digits[0]+'" />'+'<img src="Dual_Numbers/'+imageclock.digits[parseInt(sections[i])]+'" />'
else if(sections[i].length!=1 && sections[i] != sections[0])