Javascript 如果陈述总是错误的,为什么?
不管HTML和CSS如何,我已经编写了我的JS,并尽可能地对其进行了注释,以使其易于理解。基本上,这是一个颜色猜测游戏,出于某种原因,我无法制定出正常工作的逻辑,有人能找出错误吗Javascript 如果陈述总是错误的,为什么?,javascript,debugging,if-statement,logic,Javascript,Debugging,If Statement,Logic,不管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(
//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.
}
})
}
//用于去毛刺的RGB颜色数组
变量颜色=[
“RGB(255,0,0)”,
“RGB(255,255,0)”,
“RGB(255,0255)”,
“RGB(0,255,255)”,
“RGB(0,255,0)”,
“RGB(0,0,255)”]
//获得所有方形类div并放入“方形”中
var squares=document.queryselectoral(“.squares”);
//选择设置颜色作为调试所需的正确颜色
var pickedColor=颜色[3];
//马赫HTML标记
//******************************************************
var colorDisplay=document.getElementById(“colorDisplay”);
colorDisplay.textContent=pickedColor;
//******************************************************
//在所有方块中循环并指定
//“颜色”数组中的一组颜色
对于(变量i=0;i
此.style.background包含不同的信息,如颜色、图像、重复等
pickedColor
是一个只有“RGB(x,y,z)”的字符串
正确的问题是:为什么它们应该是相同的
更多关于this.style.background的信息
编辑:
在实践中检查您的RGB
在执行分配squares[i].style.background=colors[i]时会转换为RGB
您正在为样式分配字符串,如“RGB(255,0,0)”
,浏览器正在将字符串规范化为字符串,如“RGB(255,0,0)”
。如果您只是使用rgb
而不是rgb
,它将起作用
另一个问题是您没有定义white
下面是修复了错误的代码
.广场{
宽度:150px;
高度:150像素;
浮动:左;
}
var white=“RGB(255,255,255)”;//添加白色变量。
变量颜色=[
“RGB(255,0,0)”,
“RGB(255,255,0)”,
“RGB(255,0255)”,
“RGB(0,255,255)”,
“RGB(0,255,0)”,
RGB(0,0,255)
];
var squares=document.queryselectoral(“.squares”);
var pickedColor=颜色[3];
var colorDisplay=document.getElementById(“colorDisplay”);
colorDisplay.textContent=pickedColor;
对于(变量i=0;i
我想出来了
出于某种原因,我应该写rgb而不是rgb,这导致了另一个错误,我很快就发现了,我写的是白色作为变量,而不是“白色”作为字符串 console.log([clickedColor,pickedColor]),浏览器可以解析并将其转换为另一种格式,可以去除空白,也可以是不同的对象。选中this.style.backgroundColor
this.style.background指的是方形[i],当我单击颜色[i]时,它指的是颜色[i]在这个case@DaniRashba你试过在控制台上打印吗?@DaniRashba我选中了,它转换成“rgb(100100100)”,小写。