伙计们,我正在玩 HTML5 和 javascript。我正在制作的当前内容如下:屏幕上有一个紫色块,当您单击一个按钮时,它会向右移动 100 个像素。到目前为止,这有效,但是,该功能仅在第一次运行时有效。我找不到我的错误。我正在发布整个源代码(javascript、html 和 css)
<!doctype html>
<head>
<style>
#stage{
position: relative;
width : 800px;
height : 600px;
background: #c0c0c0;
border: 1px dashed black;
}
.coil{
width: 64px;
height: 64px;
background: #800080;
position: absolute;
top: 200px;
left: 50px;
}
</style>
</head>
<body>
<div id="stage">
<div class="coil"></div>
<button id="button1">move!</button>
</body>
<script>
var coil = document.querySelector(".coil");
var button = document.querySelector("#button1");
button.addEventListener("click", clickHandler2, false);
//why is it working correctly just once
function clickHandler2()
{
coil.style.left += 100 + "px";
}
</script>