开发者

JavaScript for loop not changing link text

I have a nested for loop inside a for loop that is supposed to change the link text to a random number between 1 and 5. The ID of the links are "aX_Y", X and Y being numbers. The links are arranged in a 4x3 square. The problem is that the random numbers for the link text is only displayed for the last row:

<!DOCTYPE html>
<html>
<head>
<title>RISK</title>
<style type="text/css" media="screen">
    a:link, a:visited {color: #eee;border:3px solid #ccc;text-decoration:none;padding:20px;}
    .one {background: #7B3B3B;}
    .two {background: #547980;}
    #status {color: #eee;padding:1px;text-align:center}
    .current {border:3px solid #000;}
</style>
<script type="text/javascript" charset="utf-8">
var xTurn = true;
var gameOver = false;
var numMoves = 0;

function newgame()
{
    var status = document.getElementById('status');
    numMoves = 0;
    gameOver = false;
    xTurn = true;
    status.innerHTML = 'Player One\'s turn';

    for(var x = 0; x < 4; x++)
    {
        for(var y = 0; y < 3; y++)
        {
            document.getElementById('a' + x + '_' + y).innerHTML = Math.floor(Math.random()*5 + 1);
            console.log('a' + x + '_' + y);
        }
    }
}
function current(selected)
{
    var status = document.getElementById('status');
    var value = selected.value; 
}
//document.getElementById("status").setAttribute("class", "two");
</script>
<meta name="viewport" content="user-scalable=no, width=device-width" />
</head>

<body onload='newga开发者_JAVA百科me();'>
<p id="status" class="one">Player One's turn</p>
<br />
<a href="#" id="a0_0" class="one current" onclick="current(this);"></a>
<a href="#" id="a1_0" class="two" onclick="current(this);"></a>
<a href="#" id="a2_0" class="two" onclick="current(this);"></a>
<a href="#" id="a3_0" class="two" onclick="current(this);"></a>
<br />

<a href="#" id="a0_1" class="two" onclick="current(this);"></a>
<a href="#" id="a1_1" class="two" onclick="current(this);"></a>
<a href="#" id="a2_1" class="two" onclick="current(this);"></a>
<a href="#" id="a3_1" class="two" onclick="current(this);"></a>
<br />

<a href="#" id="a0_2" class="two" onclick="current(this);"></a>
<a href="#" id="a1_2" class="two" onclick="current(this);"></a>
<a href="#" id="a2_2" class="two" onclick="current(this);"></a>
<a href="#" id="a3_2" class="two" onclick="current(this);"></a>
<br /><br />

<p><input type="button" id="newgame" value="New Game" onclick="newgame();" /></p>
</body>
</html>

Here is a direct link to it:

https://dl.dropbox.com/u/750932/iPhone/risk.html


This change to your CSS fixes the issue:

a:link, a:visited 
{
    color: #eee;
    border:3px solid #ccc;
    text-decoration:none;
    display:inline-block;
    padding:20px;
}


(Tested in Firefox)

Your Javascript code is fine; all of the grid squares are getting populated with random numbers. What I am seeing instead is that each row of links is overlapping the previous row, so the numbers in the previous row are being hidden.

Is the overlapping intentional?


All the random numbers are being generated correctly. The top 2 rows are just hidden due to your CSS rules. You can prove this by making the following CSS change:

Change the line that looks like this:

a:link, a:visited {color: #eee;border:3px solid #ccc;text-decoration:none;padding:20px;}

to this:

a:link, a:visited {color: #eee;border:3px solid #ccc;text-decoration:none;}

And voila, it's all working beautifully.


Heh, I'm pretty sure it is working...the other boxes are just overlapped by the ones in front and you can't see them. Firebug shows values inside all the boxes.

a {
    display:block;
    float:left;
}

br {
    clear:both;
}

...though actually those top-level elements shouldn't be restyled like that necessarily, I'd put it all in a <div id="game"></div> and make them .game a and .game br.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜