Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/420.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 设置html表中每行的列数限制_Javascript_Html_Html Table - Fatal编程技术网

Javascript 设置html表中每行的列数限制

Javascript 设置html表中每行的列数限制,javascript,html,html-table,Javascript,Html,Html Table,我可以知道限制Html表格列数的方法吗(例如,每行3列) 仅供参考,我正在使用row.insertCell()将单元格添加到与行id匹配的特定行。我希望将表中每行的单元格数限制为3个。“限制”?没有自然的限制。您必须在自己的代码中强制执行它 检查要插入的行是否已经有3个单元格,如果有,则不要添加新单元格。对于(i=0;i使用集合检查一行包含多少单元格 for(i=0;i<3;i++) row.insertCell() var row = document.getElementById('

我可以知道限制Html表格列数的方法吗(例如,每行3列)

仅供参考,我正在使用row.insertCell()将单元格添加到与行id匹配的特定行。我希望将表中每行的单元格数限制为3个。

“限制”?没有自然的限制。您必须在自己的代码中强制执行它

检查要插入的行是否已经有3个单元格,如果有,则不要添加新单元格。

对于(i=0;i使用集合检查一行包含多少单元格

for(i=0;i<3;i++)
row.insertCell()
var row = document.getElementById('row_id'),
    cells = row.cells, max = 3;
if (cells.length < max) {
    // Add cell(s) to #row_id
}
var row=document.getElementById('row\u id'),
单元格=行。单元格,最大值=3;
如果(单元格长度<最大值){
//将单元格添加到#行_id
}

javascript或html标准中没有此类限制。在插入过程中,您必须自己强制执行

一个简单的计数器就可以做到这一点

var items = ['c00', 'c01', 'c02', 'c10', 'c11', 'c12'];  //sample data

var table = document.getElementById("myTable");
var row;

for(var i = 0; i < items.length; i++){
  if(i % 3 == 0) {   //after every third cell add a new row and change the row variable to point to it
     row = table.insertRow(-1);      
  }
  var cell = row.insertCell(-1);  //simply insert the row
  cell.innerHTML = items[i];
}
var items=['c00','c01','c02','c10','c11','c12'];//示例数据
var table=document.getElementById(“myTable”);
var行;
对于(变量i=0;i

有很多方法。这实际上取决于您如何构造代码。

只需在行之前检查。InsettCell表格已经有多少单元格,如果等于3,则不插入单元格。请添加一些代码。您是在创建新表格,还是在用户操作后向表格中添加新的单元格/行?我知道没有“限制”但我想设置一个限制我想把一行中的单元格限制为3,在达到限制后再创建一行,有什么想法吗?