Я использовал учебник YouTube для этого. Я не понимаю, почему это не работает. По какой-то причине ничего не происходит, когда я нажимаю A
или D
.Невозможно выполнить перемещение объекта. Javascript
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
#container{
position: relative;
height: 600px;
width: 400px;
outline: 2px solid black;
}
#character{
position: absolute;
height: 50px;
width: 50px;
outline: 2px solid black;
background-color: #FF6600;
left: 0;
}
</style>
<body onkeydown="anim">
<div id="container">
<div id="character"></div>
</div>
<script>
var container = document.getElemnetById('container');
var character = document.getElemnetById('character');
var characterLeft = 0;
function anim(e){
if (e.keyCode == 97){
characterLeft += 2;
character.style.left = characterLeft + 'px';
}
if (e.keyCode == 100){
characterLeft -= 2;
character.style.left = characterLeft + 'px';
}
}
</script>
</body>
</head>
</html>
`
У вас есть опечатка, должно быть 'document.getElementById ("")' вместо 'document.getElemnetById ("")' – Arjun
О, спасибо. Тем не менее, не работает :( –