我在for循環中有一個嵌套for循環,它應該將鏈接文本更改爲1到5之間的一個隨機數。鏈接的ID是「aX_Y」,X和Y數字。鏈接排列在一個4x3的廣場上。問題是,對於鏈接文本的隨機數只顯示最後一行:用於循環的JavaScript不改變鏈接文本
<!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='newgame();'>
<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>
這裏是直接鏈接到它:
https://dl.dropbox.com/u/750932/iPhone/risk.html