请帮忙)为什么这个结构不起作用?增量只发生一次。
var player = document.getElementById("player");
var button = document.getElementById("button");
function moveDown(object) {
object.style.top += 32;
}
button.onclick = function() {
moveDown(player);
}
请帮忙)为什么这个结构不起作用?增量只发生一次。
var player = document.getElementById("player");
var button = document.getElementById("button");
function moveDown(object) {
object.style.top += 32;
}
button.onclick = function() {
moveDown(player);
}
尝试使用:
object.style.top = parseInt(object.style.top, 10) + 32;
代替
object.style.top+=32;
(并确保玩家position
设置为absolute
or relative
)
演示: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);
}