簡體   English   中英

設置html表中每行的列數限制

[英]set limit for the number of columns per row in a html table

我可以知道限制Html表的列數的方法是什么(例如每行3列)?

僅供參考,我正在使用row.insertCell()將單元格添加到與行id匹配的特定行。 我希望將表格中的單元格數限制為每行3個。

“限制”? 沒有自然限制。 您必須自己在自己的代碼上執行它。

檢查您插入的行是否已有3個單元格,如果有,則不添加新單元格。

使用row.cells集合來檢查行包含的單元格數。

var row = document.getElementById('row_id'),
    cells = row.cells, max = 3;
if (cells.length < max) {
    // Add cell(s) to #row_id
}
for(i=0;i<3;i++)
row.insertCell()

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];
}

有很多方法可以做到這一點。 它將真正取決於您如何構建代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM