单元控制器(行/列)

问题描述 投票:1回答:2

!!!!!!!主要目标是让玩家A选择垂直,而玩家B选择水平。!!!!!!!

请运行代码段以更好地理解我的问题!,非常感谢

我有一个唯一的单元格,其符号为S,颜色为红色,但是,如果单击任何单元格,它就会消失,而普通数字会替换它。

Q)单击表中的任何单元格时如何强制唯一单元格停留而不消失?

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');
  // update the text of the cell using the class
  document.querySelector('.uniqueCell').textContent = 'S';
};
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;
  text-align: center;
}
<div id="ff"></div>
javascript html css
2个回答
0
投票

函数showTable(...)每次都会生成表,并用新表替换为innerHtmlgetUnique仅在开始时标记单元格。

[可能的解决方案,是将var randomCell = RandomGenerator(...)存储在showTable(-1)之前,并在uniqueCell中设置类别showTable(...)


0
投票

添加

document.querySelectorAll("#ff td")[uniqueCell].innerHTML='S';

document.querySelectorAll("#ff td")[uniqueCell].className ='uniqueCell';

[uniqueCell是随机td数]

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;
  document.querySelectorAll("#ff td")[uniqueCell].innerHTML='S';
  document.querySelectorAll("#ff td")[uniqueCell].className ='uniqueCell';
  
}

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');
  uniqueCell=RandomGenerator(0, tdElements.length);
  tdElements[
    uniqueCell
  ].classList.add('uniqueCell');
  // update the text of the cell using the class
  document.querySelector('.uniqueCell').textContent = 'S';
};
var 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;
  text-align: center;
}
<div id="ff"></div>
© www.soinside.com 2019 - 2024. All rights reserved.