简体   繁体   English

如何从表格中选择某个单元格

[英]How to select a certain cell from a table

I have 14 generated cells.我有 14 个生成的单元格。

How can I select a specific cell from the 14 cells and control it?如何从 14 个单元格中选择特定单元格并对其进行控制? I want only one cell to be unique, the rest to be how they are displayed in the code, normally.我只希望一个单元格是唯一的,其余的是它们在代码中的显示方式,通常。

 var isCol = 0; var board = []; for (r = 0; r < 7; r++) { var line = []; for (c = 0; c < 7; c++) { line.push(r); } 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 += RandomGenerator(50, 500); 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);
 td { border: 2px solid black; width: 10px; height: 10px; } td:hover { background-color: lightgreen; } .grn { background-color: green; color: white; }
 <div id='ff'></div>

You can create a ID for that row您可以为该行创建一个 ID

EG例如

<td id = "uniqueCell"> ... </td>

Then in your javascript you can do the following.然后在您的 javascript 中,您可以执行以下操作。

var uniqueCell = document.getElementById('uniqueCell');

then use that variable to do what you need to do然后使用该变量来做你需要做的事情

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

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