"切换"用纯Javascript的CSS颜色

时间:2016-10-19 17:58:46

标签: javascript css

我正在制作一个简单的连续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>

2 个答案:

答案 0 :(得分:0)

你可以这样做。

&#13;
&#13;
//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");
  }
});
&#13;
.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;
}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您可能想要改变切换方式。不要使用2个单独的函数,只需对单击处理程序执行以下操作,无需单击任何图块。

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/