<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
.box {
100px;
height: 100px;
background-color: goldenrod;
position: absolute;
left: 0;
}
</style>
</head>
<body>
<button >按钮1</button>
<button >按钮2</button>
<div class="box" ></div>
</body>
</html>
<script>
var btn1=document.getElementById("btn1");
var btn2=document.getElementById("btn2");
var box=document.getElementById("box");
function move(speed) {
return function () {
box.style.left=box.offsetLeft+speed+"px";
}
}
btn1.onclick=move(5);
btn2.onclick=move(-5);
</script>