<!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()",10) ; } startCount(); </script> </head> <body> <form> <input type="text" id="count" /> <script>//startCount();</script> </form> </body> </html>
你的标签都是有错误的【检查head标签】
我也遇到了这个问题。我猜是因为后面的表单还没有被加载到,也就是text里面的内容根本还没读到。如果使用延时就可以等加载完读取到了。
<!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,100);
}
setTimeout(startCount,100);
</script>
</head>
<body>
<form>
<input type="text" id="count" />
</form>
</body>
</html>