<!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)
}
setTimeout("startCount()",1000)
</script>
</head>
<body>
<form>
<input type="text" id="count" onClick="startCount()"/>
</form>
</body>
</html>
在function的大括号外再写一遍 setTimeout("startCount()",1000)计时器才会正常运行
但是在form里加个button 把startCount()放在button里 就只写一遍就可以 这是为什么?
</head>
<script type="text/javascript">
var num=0;
function startCount() {
document.getElementById('count').value=num;
num=num+1;
setTimeout("startCount()",1000)
}
setTimeout("startCount()",1000)
</script>
</head>
<body>
<form>
<input type="text" id="count" />
<input type="button" value="开始计时" onClick="startCount()"/>
</form>
</body>
就像上面的代码 点开始计时就可以计时了
求解答
相关分类