<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>计时器</title>
<script type="text/javascript">
var num=0;
function startCount() {
document.getElementById('count').value=num;
num=num+1;
setTimeout(startCount(),1000);
}
startCount();
</script>
</head>
<body>
<form>
<input type="text" id="count" />
</form>
</body>
</html>
为什么13行直接写startCount()不行呢?提示报错Uncaught TypeError: Cannot set property 'value' of null
。我在html标签下方写一个script标签,里面写startCount()
又可以了,或者13行写成setTimeout(startCount(),1000);
也是可以运行的,这是为什么呢?