qq_无猜_0
2015-04-24 18:08
<!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();
</script>
</head>
<body>
<form>
<input type="text" id="count" />
</form>
</body>
</html>
页面元素还没加载 你就调用方法了···这样获取不到对应的元素
两个解决方法
1在外面加个方法 window.onload
window.onload = function(){ var num=0; function startCount() { document.getElementById('aa').value=num; num=num+1; setTimeout(startCount,1000); } startCount(); }
2 把js代码放到form 后面
<body> <form> <input type="text" id="aa" /> </form> <script type="text/javascript"> var num=0; function startCount() { document.getElementById('aa').value=num; num=num+1; setTimeout(startCount,1000); } startCount(); </script> </body>
xiexie
JavaScript进阶篇
468061 学习 · 21891 问题
相似问题