2017-04-04 105 views
1

这里我有一个文本框,它需要一个数字,然后根据点击按钮后显示随机数字的表格。这样做,现在我想要实现的基本上是添加蓝色或红色,如果单元格内的随机数是(num%3 == 0)或(num%2 == 0)。然后再显示表格下方所有数字的平均值。我一直坚持这一段时间,所以我想我寻求帮助。关于如何处理这个问题的任何提示?根据条件指定表格单元格的颜色 - JavaScript

var min = 1; 
 
var max = 100; 
 

 
function drawTable() { 
 
    //Get the div reference for the body 
 
    var div1 = document.getElementById('tableDiv'); 
 

 
    //Creates a table element 
 
    var tbl = document.createElement("table"); 
 

 
    var totalRows = parseInt(document.getElementById("inputBox").value); 
 
    var cellsInRow = parseInt(document.getElementById("inputBox").value); 
 

 
    //Creating rows 
 
    for (var rows = 0; rows < totalRows; rows++) { 
 
    var row = document.createElement("tr"); 
 

 
    /*if (rows % 3 == 0) 
 
    { 
 
     //background 
 
     bg = "class='red'";  
 

 
    } 
 
    else { 
 
     bg = "class='blue'"; 
 
    }*/ 
 

 
    //Create cells in row 
 
    for (var cells = 0; cells < cellsInRow; cells++) { 
 

 
     var cell = document.createElement("td"); 
 
     getRandom = Math.floor(Math.random() * (max - min + 1)) + min; 
 

 
     var cellText = document.createTextNode(Math.floor(Math.random() * (max - min + 1)) + min); 
 

 
     cell.appendChild(cellText); 
 
     row.appendChild(cell); 
 
    } 
 
    //Add the row to the end of the table body 
 
    tbl.appendChild(row); 
 
    } 
 
    //Appends <table> into the <div> 
 
    div1.appendChild(tbl); 
 
}
<input type="text" value="" id="inputBox"> 
 
<input type="button" value="Generate Grid" id="generateBtn" onclick="drawTable()"> 
 

 
<div id="tableDiv"> 
 
</div>

+0

我刚刚更新你的问题包括MCVE。在提出问题之前,请记得检查您的代码是否真正运行 - 我必须在代码末尾添加缺少的结束括号。 – Terry

+0

@Terry对不起,我没有注意到我没有包括大括号。谢谢 – Maxlong007

回答

2

这应该工作 - 我们增加了一个变量背景色存储迭代数的总和(后来计算平均值),并设置适当的颜色,同时遍历节点。

var min = 1; 
 
var max = 100; 
 

 
function drawTable() { 
 
    var div1 = document.getElementById('tableDiv'); 
 

 
    var tbl = document.createElement("table"); 
 

 
    var totalRows = parseInt(document.getElementById("inputBox").value); 
 
    var cellsInRow = parseInt(document.getElementById("inputBox").value); 
 

 
    var sum = 0; // Store sum of numbers 
 

 
    for (var rows = 0; rows < totalRows; rows++) { 
 
    var row = document.createElement("tr"); 
 

 
    for (var cells = 0; cells < cellsInRow; cells++) { 
 

 
     var cell = document.createElement("td"); 
 
     var randomNum = Math.floor(Math.random() * (max - min + 1)) + min; 
 
     sum += randomNum; // Increment sum 
 
     if (randomNum % 3 === 0) { 
 
      cell.setAttribute("style", "background-color:red") 
 
     } 
 
     else if (randomNum % 2 === 0) { 
 
      cell.setAttribute("style", "background-color:lightblue") 
 
     } 
 

 
     var cellText = document.createTextNode(randomNum); 
 

 
     cell.appendChild(cellText); 
 
     row.appendChild(cell); 
 
    } 
 
    tbl.appendChild(row); 
 
    } 
 
    //Appends <table> into the <div> 
 
    div1.appendChild(tbl); 
 
    var avg = sum/(totalRows * cellsInRow); // Calculate avarage 
 
    div1.appendChild(document.createTextNode("Average: " + avg)) // Add average text 
 
}
<input type="text" value="" id="inputBox"> 
 
<input type="button" value="Generate Grid" id="generateBtn" onclick="drawTable()"> 
 

 
<div id="tableDiv"> 
 
</div>

+0

谢谢Fissio。顺便说一下,无论如何,我可以让新生成的表格替换旧的表格吗?截至目前,它只是在现有的底部添加一个。就像我可以将新表添加到旧的表中,基本上替换它或者沿着这些行的东西? – Maxlong007

1

好了,下面的示例添加你想要简单地使用cell.className类。为此,它将bg计算移至for循环。

此外,您的cellText中未使用getRandom的值。我们也使用这个值来计算bg

要记住:如果要在给定代码块外使用变量的值,最好在块的顶部定义该变量,以便在其中调用它。

如果您想进一步计算出数字的平均值,则必须在将每个值创建为数组时将其保存。一旦生成所有行,就可以根据这些值计算最后一行。

希望有帮助!

var min = 1; 
 
var max = 100; 
 

 
function drawTable() { 
 
    //Get the div reference for the body 
 
    var div1 = document.getElementById('tableDiv'); 
 

 
    //Creates a table element 
 
    var tbl = document.createElement("table"); 
 

 
    var totalRows = parseInt(document.getElementById("inputBox").value); 
 
    var cellsInRow = parseInt(document.getElementById("inputBox").value); 
 

 
    //Creating rows 
 
    for (var rows = 0; rows < totalRows; rows++) { 
 
    var row = document.createElement("tr"); 
 

 
    //Create cells in row 
 
    for (var cells = 0; cells < cellsInRow; cells++) { 
 

 
     var cell = document.createElement("td"); 
 
     var getRandom = Math.floor(Math.random() * (max - min + 1)) + min; 
 

 
     var cellText = document.createTextNode(getRandom); 
 
     var bg = ''; 
 

 
     if (getRandom % 3 == 0) 
 
     { 
 
      //background 
 
      bg = 'red'; 
 
     } 
 
     else if (getRandom % 2 == 0) { 
 
      bg = 'blue'; 
 
     } else { 
 
      bg = ''; 
 
     } 
 

 
     cell.appendChild(cellText); 
 
     cell.className = bg; 
 
     row.appendChild(cell); 
 
    } 
 
    //Add the row to the end of the table body 
 
    tbl.appendChild(row); 
 
    } 
 
    //Appends <table> into the <div> 
 
    div1.appendChild(tbl); 
 
}
<input type="text" value="" id="inputBox"> 
 
<input type="button" value="Generate Grid" id="generateBtn" onclick="drawTable()"> 
 

 
<div id="tableDiv"> 
 
</div>

+0

谢谢你的解释,它有助于我更多地理解。我是Js的新手,主要从YouTube教程中学习。 – Maxlong007

相关问题