<script type="text/javascript">
var num=0;
function startCount() {
document.getElementById('count').value=num;
num=num+1;
startCount();
}
setTimeout("startCount()",1000);
</script>
你没有成功启动函数:
<script type="text/javascript">
var num=0;
function startCount() {
document.getElementById('count').value=num;
num=num+1;
setTimeout(startCount,1000);
}
setTimeout(startCount,0);
或者:
<body onload="startCount()">
onload事件要放在body里才有效。
支持onload该事件的 JavaScript 对象:
image, layer, window
input不支持该事件,直接写
window.onload=function(){
startCount();
}
楼上正解,或者setTimeout(startCount,1000)放在函数体里面,外面加上window.onload=function(){
startCount()}
<!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);
}
</script>
</head>
<body>
<form>
<input type="text" id="count" onload="startCount() "/>
</form>
</body>
</html>
而且为啥没反应?