JS在页面的加载顺序问题:
JS和HTML代码在页面的生成期间的加载是存在先后的,是由上直下的。所以有的时候JS莫名其妙的报错,而且代码也没有问题。
例子:
<head><script type="text/javascript">
document.form1.txt.value = "f"
</script></head>
<body>
<form name = "form1">
<input type = "text" name = "txt"/>
</form>
</body>
他报的错是 document.form1.txt为空或不是对象·!这是为什么呢?
解释:这是和HTML的加载页面顺序有关!document.form1.txt为空那是因为后面<body >里的内容还没有生成。所以找不到txt的存在。
<body>
<form name = "form1">
<input type = "text" name = "txt"/>
</form>
</body>
<script type="text/javascript">
document.form1.txt.value = "f"
</script>
就正常了!
所以大家在写JS时要格外注意其这个特点!