2017-04-04 109 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

相關問題