简体   繁体   English

尝试调用函数时出现Javascript错误

[英]Javascript error while trying to call a function

I continuously get an error while trying to call a function in javascript. 尝试在javascript中调用函数时,我不断收到错误消息。 The code is as follows: 代码如下:

 <!DOCTYPE html> <html> <head> <title>Tic Tac Toe</title> <SCRIPT TYPE="TEXT/JAVASCRIPT"> var xTurn = true; var gameOver = false; var numMoves = 0; 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 (gameOver) { alert("The game is already over."); return; } if (value != 'X' && value != 'O') { if (xTurn) { numMoves++; square.value = 'X'; xTurn = false; status.innerHTML = 'O\\'s turn'; } else { numMoves++; square.value = 'O'; xTurn = true; status.innerHTML = 'X\\'s turn'; } else alert('That square has already been played.'); } var winner = checkWin(); if (!winner) { //check to see if there is a tie if (numMoves == 9) status.innerHTML = 'Tie Game!'; } else gameOver = true; } 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 = ' '; } } } function checkWin() { var status = document.getElementById('status'); var val0; var val1; var val2; // check columns for (var y = 0; y < y++) { val0 = document.getElementById('0_' + y).value; val1 = document.getElementById('1_' + y).value; val2 = document.getElementById('2_' + y).value; if (val0 == 'X' && val1 == 'X' && val2 == 'X') { status.innerHTML = "X WINS!"; return true; } else if (val0 == 'O' && val1 == 'O' && val2 == 'O') { status.innerHTML = "O WINS!"; return true; } } // check rows for (var x = 0; x < x++) { val0 = document.getElementById(x + '_0').value; val1 = document.getElementById(x + '_1').value; val2 = document.getElementById(x + '_2').value; if (val0 == 'X' && val1 == 'X' && val2 == 'X') { status.innerHTML = "X WINS!"; return true; } else if (val0 == 'O' && val1 == 'O' && val2 == 'O') { status.innerHTML = "O WINS!"; return true; } } // check top left to lower right diagonal val0 = document.getElementById('0_0').value; val1 = document.getElementById('1_1').value; val2 = document.getElementById('2_2').value; if (val0 == 'X' && val1 == 'X' && val2 == 'X') { status.innerHTML = "X WINS!"; return true; } else if (val0 == 'O' && val1 == 'O' && val2 == 'O') { status.innerHTML = "O WINS!"; return true; } // check lower left to top right diagonal val0 = document.getElementById('2_0').value; val1 = document.getElementById('1_1').value; val2 = document.getElementById('0_2').value; if (val0 == 'X' && val1 == 'X' && val2 == 'X') { status.innerHTML = "X WINS!"; return true; } else if (val0 == 'O' && val1 == 'O' && val2 == 'O') { status.innerHTML = "O WINS!"; return true; } // no winner yet return false; } </script> </head> <body> <h1 style="text-align:center">Tic Tac Toe</h1> <p style="text-align:center">Tic-tac-toe is a paper-and-pencil game for two players, X and O, who take turns marking the spaces in a 3×3 grid.</p> <p style="text-align:center">The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row wins the game.</p> <p style="text-align:center">Now YOU can play the classic game, but with a twist... Your opponent... IS A COMPUTER!</p> <INPUT TYPE="BUTTON" ID="NEWGAME" VALUE="New Game" ONCLICK="newgame();"> <INPUT TYPE="BUTTON" ID="0_0" VALUE=" " ONCLICK="squareclicked(this);"> <INPUT TYPE="BUTTON" ID="1_0" VALUE=" " ONCLICK="squareclicked(this);"> <INPUT TYPE="BUTTON" ID="2_0" VALUE=" " ONCLICK="squareclicked(this);"> <BR> <INPUT TYPE="BUTTON" ID="0_1" VALUE=" " ONCLICK="squareclicked(this);"> <INPUT TYPE="BUTTON" ID="1_1" VALUE=" " ONCLICK="squareclicked(this);"> <INPUT TYPE="BUTTON" ID="2_1" VALUE=" " ONCLICK="squareclicked(this);"> <BR> <INPUT TYPE="BUTTON" ID="0_2" VALUE=" " ONCLICK="squareclicked(this);"> <INPUT TYPE="BUTTON" ID="1_2" VALUE=" " ONCLICK="squareclicked(this);"> <INPUT TYPE="BUTTON" ID="2_2" VALUE=" " ONCLICK="squareclicked(this);"> <BR> <INPUT TYPE="BUTTON" ID="0_0" VALUE="Click for a cookie" ONCLICK="alert('Cookie');"> <P ID="status">X's turn</P> </body> </html> 

If you press any of the buttons on the attached code, you get an unhandled error. 如果按所附代码上的任何按钮,则会收到未处理的错误。 The error i am getting according to Chrome Inspect Element If it matters, I am running this on Appache2, on the operating system Rasbian (latest version) being ran on a Raspberry Pi Model B Thank you in advance 我根据Chrome Inspect Element遇到的错误如果很重要,我将在Raspberry Pi Model B上运行的操作系统Rasbian(最新版本)上的Appache2上运行此错误。

var winner = checkWin();
  if (!winner) {
    //check to see if there is a tie
    if (numMoves == 9)
      status.innerHTML = 'Tie Game!';
  } else
    gameOver = true;
}

You forgot the brackets around the inner if: 在以下情况下,您忘记了内部的括号:

var winner = checkWin();
  if (!winner) {
    //check to see if there is a tie
    if (numMoves == 9){
      status.innerHTML = 'Tie Game!';
    }
  } else
    gameOver = true;
}

Also, you have if...else...else which does not make sense: 此外,您还有if ... else ... else,这没有意义:

  if (value != 'X' && value != 'O') {
    if (xTurn) {
      numMoves++;
      square.value = 'X';
      xTurn = false;
      status.innerHTML = 'O\'s turn';
    } else {
      numMoves++;
      square.value = 'O';
      xTurn = true;
      status.innerHTML = 'X\'s turn';
    } else
      alert('That square has already been played.');
  }

I think this is what you mean: 我认为这是您的意思:

  if (value != 'X' && value != 'O') {
    if (xTurn) {
      numMoves++;
      square.value = 'X';
      xTurn = false;
      status.innerHTML = 'O\'s turn';
    } else {
      numMoves++;
      square.value = 'O';
      xTurn = true;
      status.innerHTML = 'X\'s turn';
    }
  } else {
      alert('That square has already been played.');
  }

You are writing your for-loops wrong. 您正在编写for循环错误。

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

should be 应该

for (var x = 0; x < <number>; x++) {
    for (var y = 0; y < <number>; y++) {
        document.getElementById(x+"_"+y).value=" ";
    }
}

first your if else statement is wrong . 首先你的if else陈述是错误的。 Second according to the error there is some problem in calling this function . 其次,根据错误,调用此函数存在一些问题。 Please check that too . 请检查一下。

The problem is you're not writing your for loops correctly: 问题是您没有正确编写for循环:

They should be written as: 它们应写为:

for (var x = 0; x < 2; x++)

  var xTurn = true; var gameOver = false; var numMoves = 0; 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 (gameOver) { alert("The game is already over."); return; } if (value != 'X' && value != 'O') { if (xTurn) { numMoves++; square.value = 'X'; xTurn = false; status.innerHTML = 'O\\'s turn'; } else { numMoves++; square.value = 'O'; xTurn = true; status.innerHTML = 'X\\'s turn'; } } else alert('That square has already been played.'); var winner = checkWin(); if (!winner) { //check to see if there is a tie if (numMoves == 9) status.innerHTML = 'Tie Game!'; } else gameOver = true; } function newgame() { var status = document.getElementById('status'); xTurn = true; status.innerHTML = 'X\\'s turn'; for (var x = 0; x < 2; x++) { for (var y = 0; y < 2; y++) { document.getElementById(x + '_' + y).value = ' '; } } } function checkWin() { var status = document.getElementById('status'); var val0; var val1; var val2; // check columns for (var y = 0; y <2; y++) { val0 = document.getElementById('0_' + y).value; val1 = document.getElementById('1_' + y).value; val2 = document.getElementById('2_' + y).value; if (val0 == 'X' && val1 == 'X' && val2 == 'X') { status.innerHTML = "X WINS!"; return true; } else if (val0 == 'O' && val1 == 'O' && val2 == 'O') { status.innerHTML = "O WINS!"; return true; } } // check rows for (var x = 0; x < 2; x++) { val0 = document.getElementById(x + '_0').value; val1 = document.getElementById(x + '_1').value; val2 = document.getElementById(x + '_2').value; if (val0 == 'X' && val1 == 'X' && val2 == 'X') { status.innerHTML = "X WINS!"; return true; } else if (val0 == 'O' && val1 == 'O' && val2 == 'O') { status.innerHTML = "O WINS!"; return true; } } // check top left to lower right diagonal val0 = document.getElementById('0_0').value; val1 = document.getElementById('1_1').value; val2 = document.getElementById('2_2').value; if (val0 == 'X' && val1 == 'X' && val2 == 'X') { status.innerHTML = "X WINS!"; return true; } else if (val0 == 'O' && val1 == 'O' && val2 == 'O') { status.innerHTML = "O WINS!"; return true; } // check lower left to top right diagonal val0 = document.getElementById('2_0').value; val1 = document.getElementById('1_1').value; val2 = document.getElementById('0_2').value; if (val0 == 'X' && val1 == 'X' && val2 == 'X') { status.innerHTML = "X WINS!"; return true; } else if (val0 == 'O' && val1 == 'O' && val2 == 'O') { status.innerHTML = "O WINS!"; return true; } // no winner yet return false; } 
 <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="style.css"> <script src="script.js"></script> </head> <body> <h1 style="text-align:center">Tic Tac Toe</h1> <p style="text-align:center">Tic-tac-toe is a paper-and-pencil game for two players, X and O, who take turns marking the spaces in a 3×3 grid.</p> <p style="text-align:center">The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row wins the game.</p> <p style="text-align:center">Now YOU can play the classic game, but with a twist... Your opponent... IS A COMPUTER!</p> <INPUT TYPE="BUTTON" ID="NEWGAME" VALUE="New Game" ONCLICK="newgame();"> <INPUT TYPE="BUTTON" ID="0_0" VALUE=" " ONCLICK="squareclicked(this);"> <INPUT TYPE="BUTTON" ID="1_0" VALUE=" " ONCLICK="squareclicked(this);"> <INPUT TYPE="BUTTON" ID="2_0" VALUE=" " ONCLICK="squareclicked(this);"> <BR> <INPUT TYPE="BUTTON" ID="0_1" VALUE=" " ONCLICK="squareclicked(this);"> <INPUT TYPE="BUTTON" ID="1_1" VALUE=" " ONCLICK="squareclicked(this);"> <INPUT TYPE="BUTTON" ID="2_1" VALUE=" " ONCLICK="squareclicked(this);"> <BR> <INPUT TYPE="BUTTON" ID="0_2" VALUE=" " ONCLICK="squareclicked(this);"> <INPUT TYPE="BUTTON" ID="1_2" VALUE=" " ONCLICK="squareclicked(this);"> <INPUT TYPE="BUTTON" ID="2_2" VALUE=" " ONCLICK="squareclicked(this);"> <BR> <INPUT TYPE="BUTTON" ID="0_0" VALUE="Click for a cookie" ONCLICK="alert('Cookie');"> <P ID="status">X's turn</P> </body> </html> 

 <!DOCTYPE html> <html> <head> <title>Tic Tac Toe</title> <SCRIPT TYPE="TEXT/JAVASCRIPT"> var xTurn = true; var gameOver = false; var numMoves = 0; 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 (gameOver) { alert("The game is already over."); return; } if (value != 'X' && value != 'O') { if (xTurn) { numMoves++; square.value = 'X'; xTurn = false; status.innerHTML = 'O\\'s turn'; } else { numMoves++; square.value = 'O'; xTurn = true; status.innerHTML = 'X\\'s turn'; } else { // <=======================maybe?====================== alert('That square has already been played.'); } var winner = checkWin(); if (!winner) { //check to see if there is a tie if (numMoves == 9) status.innerHTML = 'Tie Game!'; } else gameOver = true; } 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 = ' '; } } } function checkWin() { var status = document.getElementById('status'); var val0; var val1; var val2; // check columns for (var y = 0; y < y++) { val0 = document.getElementById('0_' + y).value; val1 = document.getElementById('1_' + y).value; val2 = document.getElementById('2_' + y).value; if (val0 == 'X' && val1 == 'X' && val2 == 'X') { status.innerHTML = "X WINS!"; return true; } else if (val0 == 'O' && val1 == 'O' && val2 == 'O') { status.innerHTML = "O WINS!"; return true; } } // check rows for (var x = 0; x < x++) { val0 = document.getElementById(x + '_0').value; val1 = document.getElementById(x + '_1').value; val2 = document.getElementById(x + '_2').value; if (val0 == 'X' && val1 == 'X' && val2 == 'X') { status.innerHTML = "X WINS!"; return true; } else if (val0 == 'O' && val1 == 'O' && val2 == 'O') { status.innerHTML = "O WINS!"; return true; } } // check top left to lower right diagonal val0 = document.getElementById('0_0').value; val1 = document.getElementById('1_1').value; val2 = document.getElementById('2_2').value; if (val0 == 'X' && val1 == 'X' && val2 == 'X') { status.innerHTML = "X WINS!"; return true; } else if (val0 == 'O' && val1 == 'O' && val2 == 'O') { status.innerHTML = "O WINS!"; return true; } // check lower left to top right diagonal val0 = document.getElementById('2_0').value; val1 = document.getElementById('1_1').value; val2 = document.getElementById('0_2').value; if (val0 == 'X' && val1 == 'X' && val2 == 'X') { status.innerHTML = "X WINS!"; return true; } else if (val0 == 'O' && val1 == 'O' && val2 == 'O') { status.innerHTML = "O WINS!"; return true; } // no winner yet return false; } </script> </head> <body> <h1 style="text-align:center">Tic Tac Toe</h1> <p style="text-align:center">Tic-tac-toe is a paper-and-pencil game for two players, X and O, who take turns marking the spaces in a 3×3 grid.</p> <p style="text-align:center">The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row wins the game.</p> <p style="text-align:center">Now YOU can play the classic game, but with a twist... Your opponent... IS A COMPUTER!</p> <INPUT TYPE="BUTTON" ID="NEWGAME" VALUE="New Game" ONCLICK="newgame();"> <INPUT TYPE="BUTTON" ID="0_0" VALUE=" " ONCLICK="squareclicked(this);"> <INPUT TYPE="BUTTON" ID="1_0" VALUE=" " ONCLICK="squareclicked(this);"> <INPUT TYPE="BUTTON" ID="2_0" VALUE=" " ONCLICK="squareclicked(this);"> <BR> <INPUT TYPE="BUTTON" ID="0_1" VALUE=" " ONCLICK="squareclicked(this);"> <INPUT TYPE="BUTTON" ID="1_1" VALUE=" " ONCLICK="squareclicked(this);"> <INPUT TYPE="BUTTON" ID="2_1" VALUE=" " ONCLICK="squareclicked(this);"> <BR> <INPUT TYPE="BUTTON" ID="0_2" VALUE=" " ONCLICK="squareclicked(this);"> <INPUT TYPE="BUTTON" ID="1_2" VALUE=" " ONCLICK="squareclicked(this);"> <INPUT TYPE="BUTTON" ID="2_2" VALUE=" " ONCLICK="squareclicked(this);"> <BR> <INPUT TYPE="BUTTON" ID="0_0" VALUE="Click for a cookie" ONCLICK="alert('Cookie');"> <P ID="status">X's turn</P> </body> </html> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM