简体   繁体   English

在生成的表格中创建唯一的单元格

[英]Create unique cell in generated table

the table was refreshing before, now it doesn't.桌子以前很清爽,现在不行了。 now, check the new code snippet, and what the main goal i'm trying to achieve (explained in the last comment), i believe this new snippet will help clarify what i'm looking for.现在,检查新的代码片段,以及我试图实现的主要目标(在最后一条评论中解释),我相信这个新片段将有助于阐明我正在寻找的内容。 im sorry for troubling you a lot but really appreciated the help :D很抱歉给您带来了很多麻烦,但非常感谢您的帮助: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);
 td { border: 2px solid black; width: 10px; height: 10px; } td:hover { background-color: lightgreen; } .grn { background-color: green; color: white; }
 <div id="ff"></div>

I'm not sure what the point of "clicking" on one of the cells is.我不确定在其中一个单元格上“单击”的意义是什么。 So lets keep your code as it is.所以让我们保持你的代码原样。 Only change is the last few lines on how to select/set a unique table cell as unique...唯一的变化是关于如何将唯一表格单元格选择/设置为唯一的最后几行...

 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; getUnique(); } 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); /* keep your code as is for now and add the following lines */ 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: fuchsia; }
 <div id='ff'></div>

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

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