简体   繁体   English

我怎样才能阻止表格刷新

[英]How I can stop the table from refreshing

run the code snippet to have better understanding of what im trying to say I have 49 cells that have randomized numbers as you can see, and every time i click on on of these cells, the numbers change and refresh, I want the numbers in the cells to never change unless i refresh the page or have button specifically for it, otherwise they shouldnt refresh is my goal运行代码片段以更好地理解我想说的内容 我有 49 个单元格,如您所见,这些单元格具有随机数字,每次我点击这些单元格时,数字都会改变并刷新,我想要数字在单元格永远不会改变,除非我刷新页面或专门为它设置按钮,否则它们不应该刷新是我的目标

 var uniqueCell = document.getElementById('uniqueCell'); function myFunction() { document.querySelector("uniqueCell").style.backgroundColor = "red"; } 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> <td id = "uniqueCell"> </td>

Since you already have the board matrix, you can fill it with random numbers and once that is done you can render the table.由于您已经有了board矩阵,您可以用随机数填充它,一旦完成,您就可以渲染表格。

 var uniqueCell = document.getElementById("uniqueCell"); function myFunction() { document.querySelector("uniqueCell").style.backgroundColor = "red"; } 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; } Since you already have the `board` matrix, you can fill it with random numbers and once that is done you can render a table.
 <div id="ff"></div> <td id="uniqueCell"></td>

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

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