“Toggle"css颜色与纯Javascript

"Toggle" css colours with pure Javascript

本文关键字:颜色 Javascript css Toggle quot      更新时间:2023-09-26

我正在制作一个简单的3行益智游戏,我理解其中的逻辑,但我一直在尝试创建我的网格。

我要做的是改变我的蓝色瓷砖的背景颜色为白色时,一个蓝色的正方形被点击,如果相同的瓷砖再次被点击,恢复颜色回到蓝色。我在纯javascript中做这个

我的问题:我希望能够尽可能多地改变颜色,我目前仅限于改变蓝色->白色,然后白色->蓝色,没有进一步的改变。从本质上讲,这会让用户陷入第三个选择。

我可以做些什么改变来防止在点击x次后颜色变化停止?

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Grid</title>
<style>
#blueTile {
background-color:blue;
}
td{
            text-align: center;
        border: 1px solid black;
        padding: 3px;
        height:50px;
        width:50px;
}
        table {
        border-collapse: collapse;
    }
</style>
</head>
body>
<script>
//6x6 array
var solutionArray = new Array(6);
    solutionArray[0] = new Array(6);
    solutionArray[1] = new Array(6);
    solutionArray[2] = new Array(6);
    solutionArray[3] = new Array(6);
    solutionArray[4] = new Array(6);
    solutionArray[5] = new Array(6);
    var tile = {};
    var blue = tile.colour = "blue";
    var white = tile.colour = "white";
    var grey = tile.colour = "grey";
solutionArray[0][0]=blue;   
solutionArray[0][1]=white;
solutionArray[0][2]=blue;
solutionArray[0][3]=blue;
solutionArray[0][4]=white;
solutionArray[0][5]=blue;
solutionArray[1][0]=white;
solutionArray[1][1]=blue;
solutionArray[1][2]=white;
solutionArray[1][3]=blue;
solutionArray[1][4]=blue;
solutionArray[1][5]=white;
solutionArray[2][0]=blue;
solutionArray[2][1]=white;
solutionArray[2][2]=blue;
solutionArray[2][3]=white;
solutionArray[2][4]=white;
solutionArray[2][5]=blue;
solutionArray[3][0]=white;
solutionArray[3][1]=blue;
solutionArray[3][2]=white;
solutionArray[3][3]=white;
solutionArray[3][4]=blue;
solutionArray[3][5]=blue;
solutionArray[4][0]=blue;
solutionArray[4][1]=blue;
solutionArray[4][2]=white;
solutionArray[4][3]=blue;
solutionArray[4][4]=white;
solutionArray[4][5]=white;
solutionArray[5][0]=blue;
solutionArray[5][1]=white;
solutionArray[5][2]=blue;
solutionArray[5][3]=white;
solutionArray[5][4]=blue;
solutionArray[5][5]=white;

    var x = document.createElement("TABLE");
    x.setAttribute("id", "gridTable");
    document.body.appendChild(x);

    for(i=0;i<6;i++)
    {
        //output the row tag
        var y = document.createElement("TR");
        y.setAttribute("id", "row"+i);
        document.getElementById("gridTable").appendChild(y)
        for(j=0;j<solutionArray.length;j++)
        {
            ///output the td tag
            var z = document.createElement("TD");
            if(solutionArray[i][j] == blue){
            z.setAttribute("id", "blueTile");
            }
            else if (solutionArray[i][j] == white){
            z.setAttribute("id", "whiteTile");
            }
            var t = document.createTextNode(solutionArray[i][j]);
            z.appendChild(t);
            document.getElementById("row"+i).appendChild(z);

        }
    }
    var blueClick = document.getElementById("blueTile");
    blueClick.addEventListener("click", switchColor);
    function switchColor(){
    blueClick.style.backgroundColor = "white";
    blueClick.addEventListener("click", switchBack);
    }
    function switchBack(){
    blueClick.style.backgroundColor = "blue";
    blueClick.addEventListener("click", switchColor);
    }

</script>
</body>
</html>

您可能想要改变切换的方式。不需要有两个独立的函数,只需为单个单击处理程序执行以下操作,而不需要单击任何tile。

function switchColor(e){
    var curColor = this.style.backgroundColor;
    if(curColor === "white"){
        this.style.backgroundColor = "blue";
    }
    else{
        this.style.backgroundColor = "white";
    }
}

同样,ID对于元素来说应该是唯一的。在你的例子中,你像使用css类一样使用它们。下面是它的一个实现。https://jsfiddle.net/6su0b0w8/

你可以这样做。

//6x6 array
var solutionArray = new Array(6);
solutionArray[0] = new Array(6);
solutionArray[1] = new Array(6);
solutionArray[2] = new Array(6);
solutionArray[3] = new Array(6);
solutionArray[4] = new Array(6);
solutionArray[5] = new Array(6);
var tile = {};
var z = "";
var blue = tile.colour = "blue";
var white = tile.colour = "white";
var grey = tile.colour = "grey";
solutionArray[0][0] = blue;
solutionArray[0][1] = white;
solutionArray[0][2] = blue;
solutionArray[0][3] = blue;
solutionArray[0][4] = white;
solutionArray[0][5] = blue;
solutionArray[1][0] = white;
solutionArray[1][1] = blue;
solutionArray[1][2] = white;
solutionArray[1][3] = blue;
solutionArray[1][4] = blue;
solutionArray[1][5] = white;
solutionArray[2][0] = blue;
solutionArray[2][1] = white;
solutionArray[2][2] = blue;
solutionArray[2][3] = white;
solutionArray[2][4] = white;
solutionArray[2][5] = blue;
solutionArray[3][0] = white;
solutionArray[3][1] = blue;
solutionArray[3][2] = white;
solutionArray[3][3] = white;
solutionArray[3][4] = blue;
solutionArray[3][5] = blue;
solutionArray[4][0] = blue;
solutionArray[4][1] = blue;
solutionArray[4][2] = white;
solutionArray[4][3] = blue;
solutionArray[4][4] = white;
solutionArray[4][5] = white;
solutionArray[5][0] = blue;
solutionArray[5][1] = white;
solutionArray[5][2] = blue;
solutionArray[5][3] = white;
solutionArray[5][4] = blue;
solutionArray[5][5] = white;
var x = document.createElement("TABLE");
x.setAttribute("id", "gridTable");
document.body.appendChild(x);
for (i = 0; i < 6; i++) {
  //output the row tag
  var y = document.createElement("TR");
  y.setAttribute("id", "row" + i);
  document.getElementById("gridTable").appendChild(y)
  for (j = 0; j < solutionArray.length; j++) {
    ///output the td tag
    var z = document.createElement("TD");
    if (solutionArray[i][j] == blue) {
      z.setAttribute("class", "blueTile tile blue");
    } else if (solutionArray[i][j] == white) {
      z.setAttribute("class", "whiteTile tile");
    }
    var t = document.createTextNode(solutionArray[i][j]);
    z.appendChild(t);
    document.getElementById("row" + i).appendChild(z);
  }
}
document.querySelector("#gridTable").addEventListener("click", function(event) {
  if (event.target.tagName === "TD" && event.target.classList.contains("blueTile")) {
    event.target.classList.toggle("blue");
  }
});
.blue {
  background-color: blue;
}
td {
  text-align: center;
  border: 1px solid black;
  padding: 3px;
  height: 50px;
  width: 50px;
}
table {
  border-collapse: collapse;
}
.tile {
  cursor: pointer;
}
.whiteTile {
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  cursor: default;
}