請(qǐng)問 我的代碼有何問題?
<!DOCTYPE?HTML> <html> <head> <meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"> <title>定時(shí)器</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>
瀏覽器出錯(cuò)提示是 :Uncaught TypeError: Cannot set property 'value' of null 第11行?document.getElementById("clock").value = attime;
2015-09-08
你?document.getElementById("clock")寫太前了,那時(shí)DOM樹還沒解析,還沒有id為clock,只要把代碼放在input標(biāo)簽后面就不會(huì)報(bào)錯(cuò)了。
2015-09-15
setInterval(clock,1000),忽略了后面有一個(gè)延遲,那時(shí)候已經(jīng)加載完成,多謝了
2015-09-08
如此看,js進(jìn)階篇,url?http://idcbgp.cn/code/1023?,的作業(yè)代碼需要調(diào)整位置?