<style>
#container {
width: 320px;
height: 400px;
border: 1px solid red;
}
#top {
width: 320px;
height: 80px;
border: 1px solid blue;
}
</style>
<script type="text/javascript" src="One.js"> </script>
</head>
<body>
<div id="top">
<button onclick="start()">Start</button>
<button onclick="stop()">Stop</button>
<button onclick="pause()">Pause</button>
</div>
<div style="position: relative;" id="container">
<canvas id="myCanvas" width="320" height="400"
style="position: absolute; left: 0; top: 0; z-index: 0;"></canvas>
<canvas id="plane" width="320" height="400"
style="position: absolute; left: 0; top: 0; z-index: 0;"></canvas>
<canvas id="buildings" width="320" height="400"
style="position: absolute; left: 0; top: 0; z-index: 0;"></canvas>
<canvas id="cloud" width="320" height="400"
style="position: absolute; left: 0; top: 0; z-index: 0;"></canvas>
<canvas id="buildings2" width="320" height="400"
style="position: absolute; left: 0; top: 0; z-index: 0;"></canvas>
<canvas id="canvas2" width="100" height="100"
style="position: absolute; left: 0; top: 0; z-index: 1;"></canvas>
</div>
所以我希望能够调用将运行画布动画的start函数。当我在html页面上有js时,我的代码完全正常工作。但是当它作为外部脚本时,我无法让它工作。谢谢
答案 0 :(得分:1)
将<script type="text/javascript" src="One.js"> </script>
放在</body>
标记之前。那会有所帮助。在运行Javascript文件之前必须加载DOM,这就是为什么最好把它放在最后,当你确定已经加载了所有东西时。