简体   繁体   English

在我的代码中检查按钮网格的背景颜色

[英]Check background colour of the grid of buttons in my code

What I'm trying to do, is get it so when all of the buttons have been turned to black, it displays the h2 text in the 'lightsoff' div. 我正在尝试做的就是得到它,以便当所有按钮都都变成黑色时,它将在“ lightsoff” div中显示h2文本。 So if any of the buttons aren't black, the text will be hidden. 因此,如果任何按钮都不为黑色,则文本将被隐藏。

I'd like to be able to do it by writing a new function that carries out the checking of the background colour. 我希望能够通过编写一个执行背景色检查的新功能来做到这一点。

 function toggle(i, j) { b = document.getElementById("but_" + i + j) t = b.innerHTML if (t == "X") { b.innerHTML = "O"; b.setAttribute("style", "color:red; background-color:yellow") } if (t == "O") { b.innerHTML = "X"; b.setAttribute("style", "color:white; background-color:black") } } function press(i, j) { toggle(i, j); if (i > 0) { toggle(i - 1, j); } if (i < 4) { toggle(i + 1, j); } if (j > 0) { toggle(i, j - 1); } if (j < 4) { toggle(i, j + 1); } } function generateGrid() { var d = document.getElementById("button-grid"); var table = document.createElement("table"); d.appendChild(table); for (var i = 0; i < 5; i++) { var row = document.createElement("tr"); for (var j = 0; j < 5; j++) { var cell = document.createElement("td"); cell.innerHTML = "<button type=button id=but_" + i + j + " onclick=\\"press(" + i + ',' + j + ")\\"" + " style=\\"color:red; background-color:yellow\\"" + ">O</button>"; row.appendChild(cell); } table.appendChild(row); } toggle(2, 2) } window.onload = function() { generateGrid(); }; 
 <center> <h1>Lights Off Puzzle</h1> <h3>Click on the buttons until they all turn black!</h3> <div id="button-grid"></div> <div id="lightsoff"> <h2>All the lights are out, you win!</h2> </div> </center> 

Use classes and count 使用班级和人数

 function toggle(i, j) { b = document.getElementById("but_" + i + j) t = b.innerHTML if (t == "X") { b.innerHTML = "O"; b.className="on" } if (t == "O") { b.innerHTML = "X"; b.className="off" } var off = document.querySelectorAll(".off").length === document.querySelectorAll("#button-grid table tr td").length; // are all off document.getElementById("lightsoff").style.display= off ? "block":"none"; // ternary shorthand for if (something) x=a; else x=b; } function press(i, j) { toggle(i, j); if (i > 0) { toggle(i - 1, j); } if (i < 4) { toggle(i + 1, j); } if (j > 0) { toggle(i, j - 1); } if (j < 4) { toggle(i, j + 1); } } function generateGrid() { var d = document.getElementById("button-grid"); var table = document.createElement("table"); d.appendChild(table); for (var i = 0; i < 5; i++) { var row = document.createElement("tr"); for (var j = 0; j < 5; j++) { var cell = document.createElement("td"); cell.innerHTML = "<button type=button id=but_" + i + j + " onclick=\\"press(" + i + ',' + j + ")\\"" + " class='red'" + ">O</button>"; row.appendChild(cell); } table.appendChild(row); } toggle(2, 2) } window.onload = function() { generateGrid(); }; 
 .on {color:red; background-color:yellow} .off {color:white; background-color:black} #lightsoff { display: none } 
 <center> <h1>Lights Off Puzzle</h1> <h3>Click on the buttons until they all turn black!</h3> <div id="button-grid"></div> <div id="lightsoff"> <h2>All the lights are out, you win!</h2> </div> </center> 

Toggle a class using classList for the state and then look at the number of elements with that class by using querySelectorAll() . 使用classList切换状态的类,然后使用querySelectorAll()查看具有该类的元素数。

 function toggle(i, j) { b = document.getElementById("but_" + i + j) b.classList.toggle("on") b.textContent = b.classList.contains("on") ? "O" : "X" } function press(i, j) { toggle(i, j); if (i > 0) { toggle(i - 1, j); } if (i < 4) { toggle(i + 1, j); } if (j > 0) { toggle(i, j - 1); } if (j < 4) { toggle(i, j + 1); } console.log("Number of on buttons is: ", document.querySelectorAll("button.on").length) } function generateGrid() { var d = document.getElementById("button-grid"); var table = document.createElement("table"); d.appendChild(table); for (var i = 0; i < 5; i++) { var row = document.createElement("tr"); for (var j = 0; j < 5; j++) { var cell = document.createElement("td"); cell.innerHTML = "<button type=button id=but_" + i + j + " onclick=\\"press(" + i + ',' + j + ")\\"" + " class='on' " + ">O</button>"; row.appendChild(cell); } table.appendChild(row); } toggle(2, 2) } window.onload = function() { generateGrid(); }; 
 td button{ color: #FFF; background-color: #000; } td button.on { color: red; background-color: #FFC; } 
 <center> <h1>Lights Off Puzzle</h1> <h3>Click on the buttons until they all turn black!</h3> <div id="button-grid"></div> <div id="lightsoff"> <h2>All the lights are out, you win!</h2> </div> </center> 

If you really want to do it without using classes than you need to loop over all the cells and see if any of them are the color. 如果您真的想在不使用类的情况下执行此操作,则需要遍历所有单元格并查看其中是否有颜色。 Checking color codes can be a pain because not all browsers return the same thing. 检查颜色代码可能很麻烦,因为并非所有浏览器都返回相同的内容。 I would just check the text. 我只是检查一下文字。

function check()
  var btns = document.querySelectorAll("button")
  for (var i=0; i<btns.length; i++) {
    if (btns[i].textContent === "O") return false
  }
  return true
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM