看板 Web_Design 關於我們 聯絡資訊
大家好 我想問一下JavaScript 即時顯示時間的問題 我參考網路和書上寫了 不過現在碰到的問題是 1.Timer執行一次以後就顯示 show_now is not defind 所以他的秒數只跳一次 囧(setTimeout和setInterval都試了Orz) 2.document.write中如果加入Html標籤 就會顯示 警告: 已棄用屬性值的 nodeValue 屬性方法。請改用 value。 警告: 已棄用屬性值的 nodeName 屬性方法。請改用 name。 這是為什麼呢 囧 因為我想要排版成自己想要的樣子,所以無果插 入標籤就會有錯誤還蠻囧的Orz 原始碼如下 <html> <head> <script language="javascript"> function show_now() { var mydate=new Date(); var year=mydate.getFullYear(); var day=mydate.getDay(); var month=mydate.getMonth(); var daym=mydate.getDate(); var Hours=mydate.getHours(); var Minutes=mydate.getMinutes(); var Seconds=mydate.getSeconds(); if (daym<10){ daym="0"+daym;} var dayarray=new Array("SUN","MON","TUE","WED","THU","FRI","STA") var montharray=new Array("1","2","3","4","5","6","7","8","9","10","11","12") document.write("<div>"+year+"年 "+montharray[month]+""+daym+"日 "+dayarray[day]+Hours+":"+Minutes+":"+Seconds+"</div>"); //setTimeout('show_now()',3000); var Timer=0; Timer=setInterval("show_now()",1000); } </script> </head> <body> <script language="javascript"> show_now(); </script> </body> </html> -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 60.249.39.28
s25g5d4:這種寫JS的風格過時了...\ 10/24 21:39
oj113068:對不起Orz 我也是圖書館找書+上網找範例的 10/24 22:05
oj113068:嗚,我改用innerHTML試試看 Q Q 10/24 22:41
kaichan:http://pastebin.com/DeGfYVPw 簡單範例 10/25 01:47
kaichan:可以看 http://www.w3schools.com/ 很多東西可以學 10/25 01:48
kaichan:之後再來用 jquery 吧 :) 10/25 01:48
B9: document.write 把程式碼內容取代掉了 10/25 21:08
B9: 把 document.write 換成其他東西試試看 10/25 21:08
oj113068:我取消掉 document.write整行以後,確實他就不出錯了Orz 10/26 12:53
oj113068:感覺好疑惑,因為看書或是找網路範例 很多都用這種作法 10/26 12:54
整個回一下好了 現在改用inner ~略 var now,year,month,day,dayw,hours,minutes,seconds,timeValue; function showtime(){ now = new Date(); year = now.getFullYear(); month = now.getMonth(); day = now.getDay(); dayw=now.getDate(); hours = now.getHours(); minutes = now.getMinutes(); seconds = now.getSeconds(); if (dayw<10){ dayw = "0"+dayw;} if (seconds<10){ seconds="0"+seconds;} var dayarray=new Array("SUN","MON","TUE","WED","THU","FRI","STA") var montharray=new Array("1","2","3","4","5","6","7","8","9","10", "11","12"); timeValue = "<div class='times'>"+" <div class='day'>"+ year+"/"+montharray[month]+ "/"+dayw+" </div>"+ "<div='hor'>"+hours+ ":"+minutes+":"+seconds+ "<span class='week'>"+ dayarray[day]+" </span>"+ "</div>"+" </div>" daynew.innerHTML = timeValue; // 火狐要用document.getElementById( "daynew").innerHTML = timeValue; setTimeout("showtime()",1000); } showtime(); ~略 <body> <span id="daynew"></span> </body> 目前是這樣 然後IE不能用 <p id="daynew"></p> 或<div id="daynew"></div> 我還不懂為什麼 翻書中Orz ※ 編輯: oj113068 來自: 60.249.39.28 (10/26 12:57) ※ 編輯: oj113068 來自: 60.249.39.28 (10/26 13:03)