在javascript中覆盖

时间:2012-04-04 12:39:43

标签: javascript

<div id='tbl'> </div>

<script lang="Javascript">
var gd = new Array(16);
var a=0;
for(a=0; a<16; a++)
    gd[a] = new Array(16);
var sd = new Array(16);
for(a=0; a<16; a++)
    sd[a] = new Array(16);

var b1=0, b2=0;
for(b1=0; b1<16; b1++)
{
    for(b2=0; b2<16; b2++)
    {
        gd[b1][b2]="A";
        sd[b1][b2]="A";
    }
}

var c1=0, c2=0;
var score=0;

ble = "<center><table>";
for(c1=0; c1<16; c1++)
{
    ble += "<tr>";
    for(c2=0; c2<16; c2++)
    {
        ble += "<td onClick='changeColor(this)'>" + gd[c1][c2] + "</td>";
    }
    ble += "</tr>";
}
ble += "</table></center>";

document.getElementById('tbl').innerHTML = ble;

function changeColor(tdd)
{
    if(tdd.bgColor=='white')
    {
        tdd.bgColor='red'; 

        alert(c1);
        /*if (gd[c1][c2] == sd[c1][c2])
            score+=5;
        else
            score-=2;*/
    }
    else
    {
        tdd.bgColor='white'; 
    }
}
function scc()
{
    document.getElementById('scf').innerHTML = score;
}
</script>
<br><br><center><button type='button' onclick='scc()'> Click to see current score</button> <div id="scf">0</div> </center>
</body>

在上面给出的代码中,有没有办法在changeColor函数中获取c1c2当前值而不是= 16.

否则,有没有办法写入文档,以便每次都不会被覆盖?

1 个答案:

答案 0 :(得分:1)

是的,您可以将它们传递给您的函数调用:

ble += "<td onClick='changeColor(this, " + c1 + ", " + c2 + ")'>" + gd[c1][c2] + "</td>";

不要忘记在函数中声明新参数:

function changeColor(tdd, c1, c2)