若陈述总是错误的,为什么
If statement is always false, WHY?
尽管有HTML和CSS,我已经编写了JS,并尽可能地对其进行了注释,使其易于理解。基本上,这是一个猜颜色的游戏,出于某种原因,我无法找到正常工作的逻辑,有人能找出错误吗?
//Array of RGB colors for debuuging purpose
var colors = [
"RGB(255, 0, 0)",
"RGB(255, 255, 0)",
"RGB(255, 0, 255)",
"RGB(0, 255, 255)",
"RGB(0, 255, 0)",
"RGB(0, 0, 255)" ]
//Get all square class divs and put inside "squares"
var squares = document.querySelectorAll(".square");
//pick a set color as the right color for debugging purpose
var pickedColor = colors[3];
//Mach HTML <span> tag
//******************************************************
var colorDisplay = document.getElementById("colorDisplay");
colorDisplay.textContent = pickedColor;
//******************************************************
//loop through all the squares and assign
// a set color from "colors" array
for(var i = 0; i < squares.length; i++) {
//Add initial colors to squares
squares[i].style.background = colors[i];
//Add click listeners to squares
squares[i].addEventListener("click", function(){
//grab color of clicked square
var clickedColor = this.style.background;
//compare color to pickedColor
if(clickedColor === pickedColor){
this.style.background = white;
} else {
this.style.background = "#232323";
//Bug: For some reason, if statment is never
//true. I can't find the problem please help.
}
})
}
当您执行分配squares[i].style.background = colors[i];
时,您正在将类似"RGB(255, 0, 0)"
的字符串分配给样式,而浏览器正在将字符串规范化为类似"rgb(255, 0, 0)"
的字符串。如果你只使用rgb
而不是RGB
,它就会起作用。
另一个问题是您没有定义white
。
这是修复了错误的代码。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
.square {
width: 150px;
height: 150px;
float: left;
}
</style>
</head>
<body>
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
<span id="colorDisplay"></span>
<script>
var white = "RGB(255, 255, 255)"; // Adding white var.
var colors = [
"RGB(255, 0, 0)",
"RGB(255, 255, 0)",
"RGB(255, 0, 255)",
"RGB(0, 255, 255)",
"RGB(0, 255, 0)",
"RGB(0, 0, 255)"
];
var squares = document.querySelectorAll(".square");
var pickedColor = colors[3];
var colorDisplay = document.getElementById("colorDisplay");
colorDisplay.textContent = pickedColor;
for(var i = 0; i < squares.length; i++) {
squares[i].style.background = colors[i];
squares[i].addEventListener("click", function(){
var clickedColor = this.style.background;
// Using lower case
if(clickedColor === pickedColor.toLowerCase()){
this.style.background = white;
} else {
this.style.background = "#232323";
}
});
}
</script>
</body>
</html>
我想通了!!!!
出于某种原因,我应该写rgb而不是rgb,这导致了另一个错误,我很快就发现了,我把white写为变量,而不是把"white"写为字符串。
this.style.background
包含不同的信息,如颜色、图像、重复等。
pickedColor
是一个只有"RGB(x,y,z)"的字符串。
正确的问题是:为什么它们应该是一样的?
这里有更多关于这个的信息。style.background
编辑:
在实践中检查您的RGB
是否转换为rgb
相关文章:
- 为什么会出现错误;未捕获的类型错误:undefined不是函数;
- 为什么忽略了eval()代码中的语法错误
- 为什么会出现错误,“UncaughtTypeError:undefined不是函数”
- 为什么jasmine期望不验证是否抛出了错误
- 为什么我得到错误IE修剪方法是't支持
- 为什么不'当单元测试出现解析错误时,我的因果报应测试会失败
- 为什么浏览器没有为语法错误抛出异常
- 我不知道为什么我的代码是错误的?又有什么错
- 为什么我会出现此错误"未捕获引用错误:未定义标题;
- 为什么当我尝试在Ionic中使用列表时会出现此JavaScript错误
- 为什么datetime.strptime在使用Django运行时会抛出错误
- 为什么foodModule没有定义错误?Angular js
- 为什么匿名函数本身就是javascript中的语法错误
- 为什么错误不能字符串化
- sqlite查询返回错误-can'我不知道为什么
- 为什么错误显示为函数体之前缺少{
- AngularJS - 为什么“错误:ng:areq Bad Argument”
- 为什么错误st.data不是jquery中的函数
- 为什么错误信息&;SyntaxError: let是一个保留标识符require.js:52:2&;在开发firefox
- React Native + Redux:为什么错误“不应该推送带有重复键”的路由