繁体   English   中英

从表格中选择一个单元格并为其指定唯一的类

[英]Select a cell from the table and giving it unique class

我有 49 个单元格(7x7 行/列),我正在尝试从 49 个单元格中创建一个唯一的单元格,正如您在运行代码段时所看到的,这 49 个单元格具有 50 到 500 之间的随机数,并且这些细胞之一具有红色。

我正在努力解决的问题是,我希望一个单元格具有符号(标记板),例如 S 或 D,而不是数字,我该如何实现?

 var isCol = 0; var board = []; for (r = 0; r < 7; r++) { var line = []; for (c = 0; c < 7; c++) { line.push(RandomGenerator(50, 500)); } board.push(line); } function prs(c, r) { showTable(c, r); isCol = (isCol + 1) % 2; } function toColor(col, row, chosen_col, chosen_row) { var ret = false; switch (isCol) { case 0: if (row == chosen_row) { ret = true; } break; case 1: if (col == chosen_col) { ret = true; } break; } return ret; } function showTable(chosen_col, chosen_row) { var str = ""; str += "<table border=1>"; for (row = 0; row < 7; row++) { str += "<tr>"; for (col = 0; col < 7; col++) { str += "<td onclick='prs(" + col + "," + row + ")'"; if (toColor(col, row, chosen_col, chosen_row)) { str += " class='grn' "; } str += ">"; str += board[row][col]; str += "</td>"; } str += "</tr>"; } str += "</table>"; document.getElementById("ff").innerHTML = str; } function RandomGenerator(min, max) { return Math.floor(Math.random() * (max - min) + min); } showTable(-1); var getUnique = function(){ var tdElements = document.querySelectorAll('#ff td'); tdElements[ RandomGenerator(0, tdElements.length) ].classList.add('uniqueCell') }; getUnique();
 td{ border:2px solid black; width:10px; height:10px; } td:hover{background-color:lightgreen;} .grn{ background-color:green; color:white; } .uniqueCell { background-color: tomato; }
 <div id="ff"></div>

这看起来很简单,因为您在元素中有一个类 ( uniqueCell )。 您可以简单地使用类 ( uniqueCell ) 定位单元格并设置innerTexttextContent属性:

document.querySelector('.uniqueCell').textContent = 'S';

 var isCol = 0; var board = []; for (r = 0; r < 7; r++) { var line = []; for (c = 0; c < 7; c++) { line.push(RandomGenerator(50, 500)); } board.push(line); } function prs(curr, c, r) { showTable(curr, c, r); isCol = (isCol + 1) % 2; } function toColor(col, row, chosen_col, chosen_row) { var ret = false; switch (isCol) { case 0: if (row == chosen_row) { ret = true; } break; case 1: if (col == chosen_col) { ret = true; } break; } return ret; } function showTable(c, chosen_col, chosen_row) { var str = ""; str += "<table border=1>"; for (row = 0; row < 7; row++) { str += "<tr>"; for (let col = 0; col < 7; col++) { str += "<td onclick='prs(this, " + col + "," + row + ")'"; if (toColor(col, row, chosen_col, chosen_row)) { if(c.textContent == board[row][col]){ str += " class=uniqueCell"; } else str += " class='grn' "; } str += ">"; if(c.textContent == board[row][col]){ str += 'S'; } else str += board[row][col]; str += "</td>"; } str += "</tr>"; } str += "</table>"; document.getElementById("ff").innerHTML = str; } function RandomGenerator(min, max) { return Math.floor(Math.random() * (max - min) + min); } showTable(-1); var getUnique = function(){ var tdElements = document.querySelectorAll('#ff td'); tdElements[ RandomGenerator(0, tdElements.length) ].classList.add('uniqueCell'); // update the text of the cell using the class document.querySelector('.uniqueCell').textContent = 'S'; }; getUnique();
 td{ border:2px solid black; width:10px; height:10px; text-align: center; } td:hover{background-color:lightgreen;} .grn{ background-color:green; color:white; } .uniqueCell { background-color: tomato; }
 <div id="ff"></div>

暂无
暂无

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

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