为什么有时候在script内直接调用函数有效,有时候却调用不了?
如下两个例子:
例子一(script内有效调用)
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>函数调用</title>
<script type="text/javascript">
function tcon()
{
alert("恭喜你学会函数调用了!");
}
tcon(); //调用函数,成功调用
</script>
</head>
<body>
</body>
</html>
例子二(script内无效调用)
<!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>