简体   繁体   English

检查所有按钮是否颜色相同,然后显示文本Javascript

[英]Check whether all buttons are same colour, then display text Javascript

I have this code here: 我在这里有此代码:

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();
};

What I want to do is display some text using html saying for example "All the boxes are black" when all the boxes have been turned black. 我想做的是当所有盒子都变成黑色时,使用html显示一些文本,例如“所有盒子都是黑色的”。 When not all the boxes are black, I want no text to display at all. 当不是所有的框都是黑色时,我根本不想显示任何文本。

If you need any more information please let me know, thanks! 如果您需要更多信息,请让我知道,谢谢!

You can use the code below to find the color of an element. 您可以使用下面的代码查找元素的颜色。

$("#test").css('color')

The code above would give you the color of something with an id of "test". 上面的代码将为您提供ID为“ test”的颜色。 You can change the id to what you would need, and just create a for loop looping through your grid. 您可以将id更改为所需的ID,只需在网格中创建for循环即可。

 var color = $('.but_00').css('background-color'); var colorsAreEqual = true; for (var i = 0; i < 5; i++) { for (var j = 0; j < 5; j++) { if ($('.but_' + i + j).css('background-color') != color) { colorsAreEqual = false; } } } if (colorsAreEqual) { //The boxes are all the same color console.log("The colors are all " + color); } else { //The boxes are not all the same color, so it displays nothing. console.log("The colors are not the same"); } 
 #button { background-color: black; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button class="but_00" id="button">1</button> <button class="but_01" id="button">1</button> <button class="but_02" id="button">1</button> <button class="but_03" id="button">1</button> <button class="but_04" id="button">1</button> <button class="but_10" id="button">1</button> <button class="but_11" id="button">1</button> <button class="but_12" id="button">1</button> <button class="but_13" id="button">1</button> <button class="but_14" id="button">1</button> <button class="but_20" id="button">1</button> <button class="but_21" id="button">1</button> <button class="but_22" id="button">1</button> <button class="but_23" id="button">1</button> <button class="but_24" id="button">1</button> <button class="but_30" id="button">1</button> <button class="but_31" id="button">1</button> <button class="but_32" id="button">1</button> <button class="but_33" id="button">1</button> <button class="but_34" id="button">1</button> <button class="but_40" id="button">1</button> <button class="but_41" id="button">1</button> <button class="but_42" id="button">1</button> <button class="but_43" id="button">1</button> <button class="but_44" id="button">1</button> 

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

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