改变球功能

时间:2015-04-06 22:14:26

标签: function

我无法让我的球换到以外的另一个角色,我做错了什么?这是完整的代码量,我有一个附加到此的html文件,它只是调用球更改按钮。我只能按下要拨打的按钮,我无法更改" "到另一个角色。

window.onload = initAll;
var moves = new Array('Up','Down','Right','Left');

function initAll() {
for (var i=0; i<moves.length; i++) {
    var eid="id_"+moves[i];
    console.log('eid='+eid);
    document.getElementById(eid).onclick =
    function() {moveBall(this);}
}


 function moveBall(e) {
    console.warn('moveBall['+e.id+']');
var ball = document.getElementById("id_ball");
var tl=GetTopLeft(ball);
console.log('tl["Top"]='+tl["Top"]+' tl["Left"]='+tl["Left"]);
switch (e.id) {
case "id_Up":
    ball.style.top = tl['Top'] - 15 + 'px';
break;
case "id_Down":
    ball.style.top = tl['Top'] + 15 + 'px';
break;
case "id_Right":
    ball.style.left = tl['Left'] + 15 + 'px';
break;
case "id_Left":
    ball.style.left = tl['Left'] - 15 + 'px';
break;
 }
}


function GetTopLeft(e) {
var x, y = 0;
//set x to e’s offsetLeft
x = e.offsetLeft;
//set y to e’s offsetTop
y = e.offsetTop;
//set e to its offsetParent
e = e.offsetParent;
//use while loop to check if e is null
//if not then add current e’s offsetLeft to x
//offsetTop to y and set e to its offsetParent
while(e != null) {
    x = parseInt(x) + parseInt(e.offsetLeft);
    y = parseInt(y) + parseInt(e.offsetTop);
    console.log(x);
    e = e.offsetParent;
}
//Return Top and Left
return {Left:x, Top:y};
}

 function changeBallChar() {
   var ball = prompt("Please enter character for ball", "*");
   {document.getElementById(id_ball).innerHTML = ball;}
}

1 个答案:

答案 0 :(得分:1)

您的Javascript缺少最终结尾括号。

请尝试以下代码:

function changeBallChar() {
    var ball = prompt("Please enter character for ball", "*");
    if(ball != null) {
        // id_ball is not defined inside this the code you posted
    }
}

在这里查看有关JS弹出窗口的更多文档:http://www.w3schools.com/js/js_popup.asp