我刚开始在学校上课,所以请原谅我完全缺乏基础知识.这个JS只有在我把它放在HTML代码之后才有效,而不是我把它放在headtag中.window.onload不应该照顾那个吗?有人可以解释一下是什么问题吗?提前致谢!
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
#field {
width: 600;
height: 600;
background-color: black;
position: relative;
}
#player {
width: 50;
height: 50;
background-color: red;
position: absolute;
left: 0px;
top: 0px;
}
</style>
<script>
var player = document.getElementById("player");
var playerLeft = 0;
var PlayerTop = 0;
function move(e) {
if (e.keyCode == 68) {
playerLeft += 10
player.style.left = playerLeft + "px";
}
if (e.keyCode == 65) {
playerLeft -= 10
player.style.left = playerLeft + "px";
} …Run Code Online (Sandbox Code Playgroud)