0

请帮忙)为什么这个结构不起作用?增量只发生一次。

var player = document.getElementById("player");
var button = document.getElementById("button");

function moveDown(object) {
    object.style.top += 32;
}

button.onclick = function() {
    moveDown(player);
}
4

2 回答 2

0

尝试使用:

object.style.top = parseInt(object.style.top, 10) + 32;

代替

object.style.top+=32;

(并确保玩家position设置为absoluteor relative

于 2012-09-03T03:16:29.903 回答
0

演示:http: //jsfiddle.net/SrQUh/1/

var player = document.getElementById("player");
var button = document.getElementById("button");

function moveDown(object) {
  object.style.top = parseInt(object.style.top || 0) + 32 + 'px';
}

button.onclick = function() {
    moveDown(player);
}​
于 2012-09-03T03:20:30.210 回答