//Uncaught Error: Pick color is not a function - I can not find the Error
一切都很好,直到我调用函数var picked color = pickColor(); 我已经在控制台中单独运行函数pickColor() - 工作正常! 请帮忙!!!
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="colorGame.css"> Color Game </head> <body> The <span id="colorDisplay">Great </span> Colors Game!!! <script src ="colorGame.js"> </body> </html> |
CSS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | /*Color Games CSS*/ /*General Styles*/ body { margin: 0; padding:0; background-color: #232323; } h1{ color:white; } #container { width:600px; margin: 0 auto; } #message { color:white; } .squares { width: 180px; height:190px; background-color: purple; float:left; margin-right: 10px; margin-bottom: 10px; border-radius: 40px; } |
彩色游戏JavaScript - 一切都很好,直到我调用函数var picked color = pickColor(); 我已经在控制台中单独运行函数pickColor() - 工作正常! 请帮忙!!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | var colors = [ "rgb(255, 0, 0)", "rgb(255, 255, 0)", "rgb(0, 255, 0)", "rgb(0, 255, 255)", "rgb(0, 0, 255)", "rgb(255, 0, 255)" ]; // Variables var squares = document.querySelectorAll(".squares"); var pickedColor = pickColor(); var colorDisplay = document.getElementById("colorDisplay"); var message = document.getElementById("message"); //Display colorDisplay.textContent = pickedColor; //test for (var i = 0; i < squares.length; i++) { squares[i].style.backgroundColor = colors[i]; squares[i].addEventListener("click",function(){ var clickedColor = this.style.backgroundColor; if (clickedColor === pickedColor){ message.textContent ="Right Color"; changeColors(clickedColor); } else { this.style.backgroundColor ="#232323"; message.textContent ="Try Again"; } }); function changeColors(color){ for (i = 0; i < squares.length; i++){ squares[i].style.backgroundColor = color; // } } function pickColor(){ var random = Math.floor(Math.random() * colors.length ); return colors[random]; } } |
在for循环之外定义
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | function changeColors(color) { for (i = 0; i < squares.length; i++) { squares[i].style.backgroundColor = color; // } } function pickColor() { var random = Math.floor(Math.random() * colors.length); return colors[random]; } var colors = [ "rgb(255, 0, 0)", "rgb(255, 255, 0)", "rgb(0, 255, 0)", "rgb(0, 255, 255)", "rgb(0, 0, 255)", "rgb(255, 0, 255)" ]; // Variables var squares = document.querySelectorAll(".squares"); var pickedColor = pickColor() var colorDisplay = document.getElementById("colorDisplay"); var message = document.getElementById("message"); //Display colorDisplay.textContent = pickedColor; //test for (var i = 0; i < squares.length; i++) { squares[i].style.backgroundColor = colors[i]; squares[i].addEventListener("click", function() { var clickedColor = this.style.backgroundColor; if (clickedColor === pickedColor) { message.textContent ="Right Color"; changeColors(clickedColor); } else { this.style.backgroundColor ="#232323"; message.textContent ="Try Again"; } }); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | body { margin: 0; padding: 0; background-color: #232323; } h1 { color: white; } #container { width: 600px; margin: 0 auto; } #message { color: white; } .squares { width: 180px; height: 190px; background-color: purple; float: left; margin-right: 10px; margin-bottom: 10px; border-radius: 40px; } C |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="colorGame.css"> Color Game </head> <body> The <span id="colorDisplay">Great </span> Colors Game!!! </body> </html> |
据我所知,到目前为止,以下原因问题请参阅评论
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | var pickedColor = pickColor(); //Global, we're calling here for (....) { //codes function pickColor(){ //Local var random = Math.floor(Math.random() * colors.length ); return colors[random]; } } |
但是,这仍然是可能的:函数changeColor()可以像本地函数一样保留在循环内部并在本地调用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | function pickColor() { var random = Math.floor(Math.random() * colors.length); return colors[random]; } var colors = [ "rgb(255, 0, 0)", "rgb(255, 255, 0)", "rgb(0, 255, 0)", "rgb(0, 255, 255)", "rgb(0, 0, 255)", "rgb(255, 0, 255)" ]; // Variables var squares = document.querySelectorAll(".squares"); var pickedColor = pickColor() var colorDisplay = document.getElementById("colorDisplay"); var message = document.getElementById("message"); //Display colorDisplay.textContent = pickedColor; //test for (var i = 0; i < squares.length; i++) { squares[i].style.backgroundColor = colors[i]; squares[i].addEventListener("click", function() { var clickedColor = this.style.backgroundColor; if (clickedColor === pickedColor) { message.textContent ="Right Color"; changeColors(clickedColor); } else { this.style.backgroundColor ="#232323"; message.textContent ="Try Again"; } }); function changeColors(color) { for (i = 0; i < squares.length; i++) { squares[i].style.backgroundColor = color; // } } } |
这是一个范围问题。 您正在
要解决此问题,只需将函数的定义移出此循环,以便
此外,您可能希望oto对
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | var colors = [ "rgb(255, 0, 0)", "rgb(255, 255, 0)", "rgb(0, 255, 0)", "rgb(0, 255, 255)", "rgb(0, 0, 255)", "rgb(255, 0, 255)" ]; // Variables var squares = document.querySelectorAll(".squares"); var pickedColor = pickColor(); var colorDisplay = document.getElementById("colorDisplay"); var message = document.getElementById("message"); //Display colorDisplay.textContent = pickedColor; //test for (var i = 0; i < squares.length; i++) { squares[i].style.backgroundColor = colors[i]; squares[i].addEventListener("click",function(){ var clickedColor = this.style.backgroundColor; if (clickedColor === pickedColor){ message.textContent ="Right Color"; changeColors(clickedColor); } else { this.style.backgroundColor ="#232323"; message.textContent ="Try Again"; } }); } function changeColors(color){ for (i = 0; i < squares.length; i++){ squares[i].style.backgroundColor = color; // } } function pickColor(){ var random = Math.floor(Math.random() * colors.length ); return colors[random]; } |