所以我一直致力于一个项目,但无论我尝试过什么,它都会不断抛出错误。下面是我在head标签中使用的javascript。
<script>
//True is x, false is o
var turn;
//declare and initialize variables
function start()
{
alert("before");
turn = true;
cells = new Array([document.getElementById("cell00"), document.getElementById("cell01"), document.getElementById("cell02")],[document.getElementById("cell10"), document.getElementById("cell11"), document.getElementById("cell12")],[document.getElementById("cell20"), document.getElementById("cell21"), document.getElementById("cell22")]);
alert("after");
turnChange();
alert("complete");
}
/**
create the function that will check, each time a cell is clicked, if the move is legal and will, if it is not legal, display an alert if the move is illegal, it should also check to see if this move is a winner, i.e., call checkWinner()
*/
function doClick(row, col)
{
var xOrO;
if(turn)
xOrO = "X";
else
xOrO = "O";
if(cells[row][col].innerHTML.length == 0){
cells[row][col].innerHTML = xOrO;
turn = !turn;
} else
alert("This space is already taken");
checkWinner();
}
/**
This function runs after every turn and displays which player's turn it is
*/
function turnChange()
{
var temp;
if(turn)
temp = "Player 1 (X)";
else
temp = "Player 2 (O)";
document.getElementById("playerTurn").innerHTML = temp;
}
/**
create function to check if the last move made makes this a win
display winning message if it is a winner
*/
function checkWinner()
{
var xWin = false;
var yWin = false;
if(var x = 0; x < 3; x++){
if(cells[i][0]);
}
for (var i = 0; i < 3; i++){
for (var j= 0; j < 3; j++){
if ()
win = false;
}
}
if (win) {
alert("Congratulations! You won!");
id (window.prompt("Play again?", "yes"));
placeNumbers();
}
}
</script>
然而,错误会出现在我的onload或onclick的代码正文中。 onload已经停止为我抛出错误,但它仍然拒绝运行代码。
<body>
<div id="container" onload="start()">
<h1 id="logo"><em>Tic Tac Toe</em></h1>
<p> </p>
<div id="content">
<p><input type="button" value = "Restart the game" id = "restartButton" onclick = "start()"/></p>
</div>
</div>
</body>
以上所有内容都在html标签中,并且所有标签都有适当的结束标签。有人有什么想法吗?
修改
抛出的错误是 未捕获的ReferenceError:未定义start 在HTMLInputElement.onclick(TTT.html:94) 我的start方法中列出的警报也不会运行,这使我相信该方法本身被忽略。
答案 0 :(得分:0)
turnChange
未定义您的错误来自何处。我还将onload
属性移动到body元素,以便它按预期工作。
checkWinner
函数似乎也没有定义,但是你的代码在进入该调用之前是错误的。
//True is x, false is o
var turn;
//declare and initialize variables
function start() {
alert("before");
turn = true;
//all cells below will be null
cells = new Array([document.getElementById("cell00"), document.getElementById("cell01"), document.getElementById("cell02")], [document.getElementById("cell10"), document.getElementById("cell11"), document.getElementById("cell12")], [document.getElementById("cell20"), document.getElementById("cell21"), document.getElementById("cell22")]);
alert("after");
turnChange(); //this function is undefined - root of problem
alert("complete");
}
function doClick(row, col) {
var xOrO;
if (turn)
xOrO = "X";
else
xOrO = "O";
if (cells[row][col].innerHTML.length == 0) {
cells[row][col].innerHTML = xOrO;
turn = !turn;
} else
alert("This space is already taken");
checkWinner(); //this doesnt appear to be defined either
}
<body onload="start()">
<!-- moved onload to body element -->
<div id="container">
<h1 id="logo"><em>Tic Tac Toe</em></h1>
<p> </p>
<div id="content">
<p><input type="button" value="Restart the game" id="restartButton" onclick="start()" /></p>
</div>
</div>
</body>