請問 我的代碼有何問題?
<!DOCTYPE?HTML> <html> <head> <meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"> <title>定時器</title> <script?type="text/javascript"> ??var?attime; ??function?clock(){ ????var?time=new?Date();?????????? ????attime?=?time.getHours()+":"+time.getMinutes()+":"+time.getSeconds()?; ???//?document.write(attime); ????document.getElementById("clock").value?=?attime; ??} ??setInterval(clock(),1000); </script> </head> <body> <form> <input?type="text"?id="clock"?size="50"?/> </form> </body> </html>
瀏覽器出錯提示是 :Uncaught TypeError: Cannot set property 'value' of null 第11行?document.getElementById("clock").value = attime;
2015-09-08
你?document.getElementById("clock")寫太前了,那時DOM樹還沒解析,還沒有id為clock,只要把代碼放在input標簽后面就不會報錯了。
2015-09-15
setInterval(clock,1000),忽略了后面有一個延遲,那時候已經加載完成,多謝了
2015-09-08
如此看,js進階篇,url?http://www.xianlaiwan.cn/code/1023?,的作業代碼需要調整位置?