实践出真知
my.html内容
<html>
<head>
<script type="text/javascript" src="top.js"></script>
<script>
alert(1);
</script>
</head>
<body>
<button onclick="buttonclick()">点击</button>
<script>
alert(2);
</script>
</body>
<script type="text/javascript" src="bottom.js"></script>
<script>
alert(i);
alert(3);
var i = 1;
</script>
</html>
top.js内容
alert("top");
bottom.js内容
alert("bottom");
alert(i);//不会alert,下方有原因描述
function buttonclick(){
alert(i);
}
运行时,alert的顺序为:top、1、2、bottom、undefined、3
点击按钮时:alert 1
注:bottom.js中第二行的alert(i)不会alert,浏览器控制台会报错
因为此时i还没有初始化