【发布时间】:2016-05-07 09:43:04
【问题描述】:
别管 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.
}
})
}
【问题讨论】:
-
console.log([clickedColor,pickColor]),浏览器可以解析并将其转换为另一种格式,可以去除空格,也可以是不同的对象。
-
检查
this.style.backgroundColor
标签: javascript debugging if-statement logic