简体   繁体   English

如何自定义表格中的表格单元格

[英]How to customize table cells in a table

I'm creating a multiplication table using JavaScript and HTML.我正在使用 JavaScript 和 HTML 创建一个乘法表。 I'm unable to properly format the table.我无法正确格式化表格。 I'm able to calculate all the numbers correctly but each number is supposed to be in a cell formatted in a certain way.我能够正确计算所有数字,但每个数字都应该位于以某种方式格式化的单元格中。 In my table, I tried to assign each cell to and "outer-cell" and "inner-cell" class so I could customize the cells in a css file but it doesn't work.在我的表格中,我尝试将每个单元格分配给“外部单元格”和“内部单元格”类,以便我可以自定义 css 文件中的单元格,但它不起作用。 I even tried customizing all the "th" and "td" tags in my css file and that didn't work.我什至尝试在我的 css 文件中自定义所有“th”和“td”标签,但没有奏效。 Any change I make just doesn't show up on the web browser.我所做的任何更改都不会显示在网络浏览器上。 For example, if in my css file I do例如,如果在我的 css 文件中我做

outer-cell {
   border-style:solid;
}

I thought that would create a box around all cells but it's not working.我认为这会在所有单元格周围创建一个框,但它不起作用。

Attached is a photo of what it looks like and what it should look like.附上一张照片,说明它的样子和应该是什么样子。 在此处输入图片说明 在此处输入图片说明

var row_min, row_max, column_min, column_max;

  console.log("hello");


function getUserInput() {

  row_min = parseInt(document.getElementById("row_min").value);
  row_max = parseInt(document.getElementById("row_max").value);
  col_min = parseInt(document.getElementById("col_min").value);
  col_max = parseInt(document.getElementById("col_max").value);

  console.log(row_min, row_max, col_min, col_max);

  generateTable();
}


/*
* Function to validate user input
*
*/

/*
 * Function to validate user input
 */
function generateTable()
{
    // clear current table
    var tableDiv = document.getElementById("table-placeholder");
    while (tableDiv.hasChildNodes()) {
      tableDiv.firstChild.remove();
    }
    console.log("here");

    var table = document.createElement("table");
    table.setAttribute("id", "multiplicationTable");
    tableDiv.appendChild(table);

    // create header row
    var newRow = document.createElement("tr");
    table.appendChild(newRow);

    // create headers in header row
    var newHeader = document.createElement("th");
    newHeader.setAttribute("class", "outer-cell");
    newRow.appendChild(newHeader);
    for (let x = col_min; x <= col_max; x++) {
      newHeader = document.createElement("th");
      newHeader.setAttribute("class", "outer-cell");
      newHeader.textContent = x;
      newRow.appendChild(newHeader);
    }

    // start filling up rows
    for (let y = row_min; y <= row_max; y++) {
      // create new row
      newRow = document.createElement("tr");
      table.appendChild(newRow);

      // add items to row
      let newCell = document.createElement("td");
      newCell.textContent = y;
      newCell.setAttribute("class", "outer-cell");
      newRow.appendChild(newCell);
      for (let x = col_min; x <= col_max; x++) {
        newCell = document.createElement("td");
        newCell.textContent = x * y;
        newCell.setAttribute("class", "inner-cell");
        newRow.appendChild(newCell);
      }
    }
}

Here's something to get you started:这里有一些东西可以让你开始:

You can fine tune it yourself.你可以自己微调。 Use inspect element in Chrome and find the right CSS element to adjust.在 Chrome 中使用检查元素并找到正确的 CSS 元素进行调整。

 var row_min, row_max, column_min, column_max; function getUserInput() { row_min = 1; row_max = 5; col_min = 1; col_max = 5; generateTable(); } /* * Function to validate user input * */ /* * Function to validate user input */ function generateTable() { // clear current table var tableDiv = document.getElementById("table-placeholder"); while (tableDiv.hasChildNodes()) { tableDiv.firstChild.remove(); } var table = document.createElement("table"); table.setAttribute("id", "multiplicationTable"); tableDiv.appendChild(table); // create header row var newRow = document.createElement("tr"); table.appendChild(newRow); // create headers in header row var newHeader = document.createElement("th"); newHeader.setAttribute("class", "outer-cell"); newRow.appendChild(newHeader); for (let x = col_min; x <= col_max; x++) { newHeader = document.createElement("th"); newHeader.setAttribute("class", "outer-cell"); newHeader.textContent = x; newRow.appendChild(newHeader); } // start filling up rows for (let y = row_min; y <= row_max; y++) { // create new row newRow = document.createElement("tr"); table.appendChild(newRow); // add items to row let newCell = document.createElement("td"); newCell.textContent = y; newCell.setAttribute("class", "outer-cell"); newRow.appendChild(newCell); for (let x = col_min; x <= col_max; x++) { newCell = document.createElement("td"); newCell.textContent = x * y; newCell.setAttribute("class", "inner-cell"); newRow.appendChild(newCell); } } } getUserInput();
 table{ border-collapse: collapse; font-family: Sans-Serif; text-align:right; } th,td{ border:2px solid black; padding: 0.5em; margin:0; } th.outer-cell, tr td:first-child{ background:black; color:white; border-color:white; }
 <div id="table-placeholder"></div>

Please try this code, To How to customize table cells in a table请试试这个代码,到如何自定义表格中的表格单元格

<html>
<head>
  <title>Multiplication Table</title>
  <script type="text/javascript">
    var rows = prompt("How many rows for your multiplication table?");
    var cols = prompt("How many columns for your multiplication table?");
    if(rows == "" || rows == null)
         rows = 10;
    if(cols== "" || cols== null)
         cols = 10;
    createTable(rows, cols);
    function createTable(rows, cols)
    {
      var j=1;
      var output = "<table border='1' width='500' cellspacing='0'cellpadding='5'>";
      for(i=1;i<=rows;i++)
      {
        output = output + "<tr>";
        while(j<=cols)
        {
          output = output + "<td>" + i*j + "</td>";
          j = j+1;
        }
         output = output + "</tr>";
         j = 1;
    }
    output = output + "</table>";
    document.write(output);
    }
  </script>
</head>
<body>
</body>
</html>

I hope this code will be useful to you.我希望这段代码对你有用。

Thank you.谢谢你。

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

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