毛线球_hh
2015-10-09 14:21
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>计时器</title>
</head>
<script type="text/javascript">
var num=0;
function startCount() {
document.getElementById('count').value=num;
num=num+1;
setTimeout("startCount()",1000);
}
startCount();
//setTimeout("startCount()",0);
</script>
</head>
<body>
<form>
<input type="text" id="count" />
</form>
</body>
</html>
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>计时器</title> </head> </head> <body> <form> <input type="text" id="count" value="" /> </form> <script type="text/javascript"> var num=0; function startCount() { document.getElementById('count').value = num; num=num+1; setTimeout("startCount()",1000); } startCount(); </script> </body> </html>
原因是你程序一开始就被执行。系统还没有获取到input这个标签。导致document.getElementById('count').value这报错。
把startCount();改成setTimeout(startCount,1000);就能执行了···具体我也不知道为什么···
JavaScript进阶篇
468061 学习 · 21891 问题
相似问题