一切都很好,直到我调用函数var picked color = pickColor();为止。我已经分别在控制台中运行功能pickColor()-正常!!!请帮忙!!!
HTML
<!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">
<title>Color Game</title>
</head>
<body>
<h1>The <span id= "colorDisplay">Great </span> Colors Game!!!</h1>
<div id="message"></div>
<div id="container">
<div class="squares"></div>
<div class="squares"></div>
<div class="squares"></div>
<div class="squares"></div>
<div class="squares"></div>
<div class="squares"></div>
</div>
<script src = "colorGame.js"></script>
</body>
</html>
CSS
/*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;
}
Color Games JavaScript-一切都很好,直到我调用函数var picked color = pickColor();为止。我已经分别在控制台中运行功能pickColor()-正常!!!请帮忙!!!
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];
}
}
答案 0 :(得分:1)
在for循环之外定义pickColor
和changeColor
函数。如果它们在循环中定义,则无法从外部访问它们。
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";
}
});
}
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
<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">
<title>Color Game</title>
</head>
<body>
<h1>The <span id="colorDisplay">Great </span> Colors Game!!!</h1>
<div id="message"></div>
<div id="container">
<div class="squares"></div>
<div class="squares"></div>
<div class="squares"></div>
<div class="squares"></div>
<div class="squares"></div>
<div class="squares"></div>
</div>
</body>
</html>
答案 1 :(得分:0)
这是一个范围问题。您正在pickColor
循环中定义函数for
,因此只能在此循环中定义和使用它。
要解决此问题,只需将函数的定义移到此循环之外,以便您的var pickedColor = pickColor();
行可以使用它。
此外,您可能希望oto对changeColors()
函数执行相同的操作。像这样:
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];
}
答案 2 :(得分:0)
据我所知,以下原因请参见评论
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()可以像本地函数一样保留在循环中并在本地调用。
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; //
}
}
}