正确的功能放置

时间:2017-01-17 00:34:46

标签: javascript

非常新的编程,并且有一个简单的问题,关于在哪里放置多个功能我试图制作一个tic tac toe游戏和我想要放置的功能



function newgame()
{
   var status = document.getElementById('status');

   xTurn = true;
   status.innerHTML = 'X\'s turn';

   for(var x = 0; x < x++)
   {
      for(var y = 0; y < y++)
      {
         document.getElementById(x + '_' + y).value = ' ';
      }
   }
}
&#13;
&#13;
&#13;

*它会在哪里

&#13;
&#13;
<!DOCTYPE html>
<html>
<head>
<SCRIPT TYPE="TEXT/JAVASCRIPT">
var xTurn = true;

function squareclicked(square)
// squareclicked is a function that is called whenever a button is clicked.
{
 var status = document.getElementById('status');
 var value = square.value;
 if(value != 'X' && value != 'O')
 {
   if(xTurn)
   {
      square.value = 'X';
      xTurn = false;
      status.innerHTML = 'O\'s turn';
   }
   else
      {
         square.value = 'O';
         xTurn = true;
         status.innerHTML = 'X\'s turn';
      }
 }
   else
      alert('That square has already been played.');
}
</SCRIPT>

<body>
<INPUT TYPE="BUTTON" ID="0_0" VALUE=" " ONCLICK="squareclicked(this);">
<INPUT TYPE="BUTTON" ID="0_0" VALUE=" " ONCLICK="squareclicked(this);">
<INPUT TYPE="BUTTON" ID="0_0" VALUE=" " ONCLICK="squareclicked(this);">
<BR>
<INPUT TYPE="BUTTON" ID="0_0" VALUE=" " ONCLICK="squareclicked(this);">
<INPUT TYPE="BUTTON" ID="0_0" VALUE=" " ONCLICK="squareclicked(this);">
<INPUT TYPE="BUTTON" ID="0_0" VALUE=" " ONCLICK="squareclicked(this);">
<BR>
<INPUT TYPE="BUTTON" ID="0_0" VALUE=" " ONCLICK="squareclicked(this);">
<INPUT TYPE="BUTTON" ID="0_0" VALUE=" " ONCLICK="squareclicked(this);">
<INPUT TYPE="BUTTON" ID="0_0" VALUE=" " ONCLICK="squareclicked(this);">
<BR>
<INPUT TYPE="BUTTON" ID="NEWGAME" VALUE="New Game" ONCLICK="newgame();">
<P ID="status">X's turn</P>
</body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

for函数中的newgame循环中存在语法错误。他们应该是:

for(var x = 0; x < 3; x++)
{
    for(var y = 0; y < 3; y++)
    {
        document.getElementById(x + '_' + y).value = ' ';
    }
}

如果您解决了这个问题,那么当您将它们放在<script>标记中时,它应该可以正常工作。

相关问题