gpt4 book ai didi

javascript - "Toggle"纯Javascript的CSS颜色

转载 作者:行者123 更新时间:2023-11-28 05:26:20 25 4
gpt4 key购买 nike

我正在开发一个简单的三排益智游戏,我理解其逻辑,但我一直在尝试创建我的网格。

我想要做的是,当单击蓝色方 block 时,将蓝色图 block 的背景颜色更改为白色,如果再次单击同一图 block ,则将颜色恢复为蓝色。我用纯 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 个单独的函数,只需对单个单击处理程序执行以下操作,无需任何图 block 单击。

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

此外,ID 对于元素来说应该是唯一的。不过,您在示例中将它们用作 css 类。这是一个关于它的实现的 fiddle 。 https://jsfiddle.net/6su0b0w8/

关于javascript - "Toggle"纯Javascript的CSS颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40138484/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com