2017-02-19 36 views
-1

工作在一個簡單的Tic Tac Toe遊戲中以更好地實現我的JS,但我堅持使用此錯誤消息。我的「console.log(」消息「);」返回應顯示的內容「遊戲已準備就緒,玩家X轉。」它的單個文件與我的腳本標籤在我的</body>標籤的最後。這很基本。任何幫助,將不勝感激。Uncaught TypeError:無法設置屬性'innerText'爲空 - 可能的語法錯誤

var Display = function(displayElement) { \t \t \t \t \t \t // create 'class' to eliminate many 'innerText' usage 
 
\t var display = displayElement; \t \t \t \t \t \t \t \t // and makes it easy to port to say android 
 
\t function setText(message) { 
 
\t \t console.log(message); 
 
\t \t display.innerText = message; 
 
\t } 
 

 
\t return {setMessage: setText}; 
 
}; 
 

 
\t function isValid(button) { \t \t \t \t \t \t \t \t \t //if text within then not valid; if 0 then valid; any 
 
\t \t return button.innerText.length == 0; \t \t \t \t \t //characters then invalid 
 
\t } 
 

 
\t function checkForWinner(squares, players, currentTurn) { 
 
\t \t if (squares[0].innerText == players[currentTurn] && 
 
\t \t \t squares[1].innerText == players[currentTurn] && 
 
\t \t \t squares[2].innerText == players[currentTurn]) 
 
\t \t \t return true; 
 
\t \t if (squares[3].innerText == players[currentTurn] && 
 
\t \t \t squares[4].innerText == players[currentTurn] && 
 
\t \t \t squares[5].innerText == players[currentTurn]) 
 
\t \t \t return true; 
 
\t \t if (squares[6].innerText == players[currentTurn] && 
 
\t \t \t squares[7].innerText == players[currentTurn] && 
 
\t \t \t squares[8].innerText == players[currentTurn]) 
 
\t \t \t return true; 
 
\t \t if (squares[0].innerText == players[currentTurn] && 
 
\t \t \t squares[3].innerText == players[currentTurn] && 
 
\t \t \t squares[6].innerText == players[currentTurn]) 
 
\t \t \t return true; 
 
\t \t if (squares[1].innerText == players[currentTurn] && 
 
\t \t \t squares[4].innerText == players[currentTurn] && 
 
\t \t \t squares[7].innerText == players[currentTurn]) 
 
\t \t \t return true; 
 
\t \t if (squares[2].innerText == players[currentTurn] && 
 
\t \t \t squares[5].innerText == players[currentTurn] && 
 
\t \t \t squares[8].innerText == players[currentTurn]) 
 
\t \t \t return true; 
 
\t \t if (squares[0].innerText == players[currentTurn] && 
 
\t \t \t squares[4].innerText == players[currentTurn] && 
 
\t \t \t squares[8].innerText == players[currentTurn]) 
 
\t \t \t return true; 
 
\t \t if (squares[2].innerText == players[currentTurn] && 
 
\t \t \t squares[4].innerText == players[currentTurn] && 
 
\t \t \t squares[6].innerText == players[currentTurn]) 
 
\t \t \t return true; 
 
\t } 
 

 
\t function setMark(button, mark) { 
 
\t \t button.innerText = mark; 
 
\t } 
 

 
\t function main() { 
 
\t \t var squares = document.querySelectorAll("#game button"); // returns array of all the buttons 
 
\t \t var players = ["X", "O"]; \t \t \t \t \t \t \t \t // text displayed in the button 
 
\t \t var currentTurn = 0; \t \t \t \t \t \t \t \t \t // either will be 0 or 1 
 
\t \t var isGameOver = false; \t \t \t \t \t \t \t \t \t // boolean either T or F 
 
\t \t var display = new Display(document.querySelector("gameActionDisplay")); //reference to msg \t \t \t \t \t 
 

 
\t \t display.setMessage("Game is ready. Player " + players[currentTurn] + " turn."); 
 

 
\t \t for (var i = 0, len = squares.length; i<len; i++) { \t \t //for loop to iterate through squares; set local var len 
 
\t \t \t squares[i].addEventListener("click", function() { 
 
\t \t \t \t if (isGameOver) 
 
\t \t \t \t \t return; 
 

 
\t \t \t \t if(!isValid(this)) { 
 
\t \t \t \t \t display.setMessage("Invalid move bud!"); 
 
\t \t \t \t } else { 
 
\t \t \t \t \t setMark(this, players[currentTurn]); 
 

 
\t \t \t \t \t isGameOver = checkForWinner(squares, players, currentTurn); 
 

 
\t \t \t \t \t //Game is over 
 

 
\t \t \t \t \t //Game is draw 
 

 
\t \t \t \t \t //Game is not over yet; play on 
 
\t \t \t \t \t currentTurn++; \t \t \t \t \t \t \t \t // mod remainder toggles between 1 and 0 \t \t \t \t 
 
\t \t \t \t \t currentTurn = currentTurn % 2; 
 

 
\t \t \t \t \t display.setMessage("Player " + players[currentTurn] + " move"); 
 

 
\t \t \t \t } 
 
\t \t \t }); 
 
\t \t } 
 
\t } 
 

 
\t main();
<body> 
 
\t <div id="game"> 
 
\t <div> 
 
\t \t <button></button> 
 
\t \t <button></button> 
 
\t \t <button></button> 
 
\t </div> 
 
\t <div> 
 
\t \t <button></button> 
 
\t \t <button></button> 
 
\t \t <button></button> 
 
\t </div> 
 
\t <div> 
 
\t \t <button></button> 
 
\t \t <button></button> 
 
\t \t <button></button> 
 
\t </div> 
 

 
\t <div id="gameActionDisplay"></div> 
 

 

 
\t </div> 
 
    </body>

+0

在您的構造函數顯示。你執行return {setMessage:setText}; - 忽略setText使用參數。 – fubbe

+0

@AndrewLi:DOM已準備就緒,因爲腳本標籤位於主體的底部,因此不使用JQuery。在投票結束之前,您是否真的回顧過問題和代碼? – LANole

+0

@LANole不要以爲我把你的問題拒絕了......我*沒有*實際上,它投下來... – Li357

回答

2

你需要你得到你的元素通過ID

document.querySelector("#gameActionDisplay") 

做的伎倆

乾杯

+0

呃...該死的'#'。我花了太多時間閱讀這段代碼,從來沒有看到過這個代碼。那樣做了。想一想,讓我的眼睛休息一下。非常感謝。 – LANole

相關問題