简体   繁体   English

如何获取JavaScript创建的动态表的CSS属性?

[英]How to get css property of dynamic table created by javascript?

This is javascript code for creating dynamic table it creates a crossword 这是用于创建动态表的JavaScript代码,它会创建填字游戏

function generateTable(range) {
    document.write("<table style='border: 1px solid black;'>");
    for (var a = 1; a < 10; a++) {
        document.write("<tr >");
        for (var b = 1; b < 10; b++) {
            if (a % 2 == 0 && b % 2 == 0) {
                document.write("<td style='border: 1px solid black;'><button style='width:50px; height:50px; background-color:black'></button></td>");
            } else {
                document.write("<td style='border: 1px solid black;'><button style='width:50px; height:50px; background-color:white' ></button></td>");
            }
        }
        document.write("</tr>");
    }
    document.write("</table>");
}

output is something like this 输出是这样的 在此处输入图片说明

I want to count the n of black boxes with different function I have tried with getelementsbytagname() but it's not working please help me with this 我想计算使用getelementsbytagname()尝试过的具有不同功能的黑匣子中的n个,但是它不起作用,请帮助我

I would suggest you to use CSS for displaying purpose. 我建议您使用CSS进行显示。 To this, you will have to add class to each of the button . 为此,您必须将class添加到每个button

 function generateTable(range) { document.write("<table>"); for (var a = 1; a < range; a++) { document.write("<tr >"); for (var b = 1; b < range; b++) { if (a % 2 == 0 && b % 2 == 0) { document.write("<td ><button class='black'></button></td>"); } else { document.write("<td ><button class='white'></button></td>"); } } document.write("</tr>"); } document.write("</table>"); } generateTable(10); console.log("total Black " + document.getElementsByClassName("black").length); //<-- count of black console.log("total white " + document.getElementsByClassName("white").length); //<-- count of white //using query selector console.log("total Black " + document.querySelectorAll(".black").length); //<-- count of black console.log("total white " + document.querySelectorAll(".white").length); //<-- count of white 
 table, td { border: 1px solid black; } button.black { width: 50px; height: 50px; background-color: black; } button.white { width: 50px; height: 50px; background-color: white; } 

Although querying by style properties is possible, I think you may be better off creating the back boxes using a class instead on inline styles like so: 虽然可以通过样式属性查询,但我认为您最好使用类而不是像这样的内联样式创建后箱:

document.write("<td style='border: 1px solid black;'><button style='box box-black'></button></td>");

Adding the required CSS: 添加所需的CSS:

.box {
  width:50px;
  height:50px;
}

.box-black {
  background-color: black;
}

And then simply querying the class you want: 然后只需查询所需的类即可:

const blackBoxes = document.querySelectorAll('.box-black').length;

 function generateTable() { document.write("<table style='border: 1px solid black;'>"); for (var a=1; a <10; a++) { document.write("<tr >"); for(var b=1; b<10; b++) { if(a%2==0 && b%2==0 ) { document.write("<td style='border: 1px solid black;'><button id='blk' style='width:50px; height:50px; background-color:black'></button></td>"); } else { document.write("<td style='border: 1px solid black;'><button style='width:50px; height:50px; background-color:white' ></button></td>"); } } document.write("</tr>"); } document.write("</table>"); console.log("BlackBox :"+ document.querySelectorAll('#blk').length) } generateTable() 

You can use this for count number of black box. 您可以将其用于黑盒的计数。 You can get elements by tag name. 您可以通过标签名称获取元素。 You can get it by className which is the best way you can easily get count. 您可以通过className来获取它,这是轻松获得计数的最好方法。

var list = document.getElementsByTagName("Button");
var count=0;
    for(var k=0;k<list.length;k++)
    {
        if(list[k].style.backgroundColor=='red')
            count++;
    }
console.log("number of black box--",count);

The advice to change to CSS is great, but if you had reason to leave it as is and still wanted to count, here you go: 更改CSS的建议很棒,但是如果您有理由将其保留为原样并且仍然想要计算,那么您可以执行以下操作:

var list = document.getElementsByTagName("BUTTON");
var whites = 0, blacks=0;
for (var i=0; i < list.length; i++) {
    color = list[i].style.backgroundColor;
    if (color == 'white') whites++;
    else if (color == 'black') blacks++;

    // to add an index: 
    // list[i].setAttribute('data-index',i+10);
}

console.log('w:'+whites+", b:"+blacks);

References: https://www.w3schools.com/jsref/met_document_getelementsbytagname.asp 参考: https : //www.w3schools.com/jsref/met_document_getelementsbytagname.asp

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

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