问题:我必须动态显示单元格,每行必须有 3 个单元格。我想找到它需要多少行,如果用户输入 7,前两行显示 3 个单元格,第三行将只显示 1 个单元格。
代码:
var cells = document.getElementsByTagName('td');
var cell = '<td>' + cells[0].innerHTML + '<td>';
//console.log(cell);
document.getElementById('searchBtn').onclick = search;
var NUMPERROW = 3;
function search(){
var num = document.getElementById('searchTxt').value;
//Loop Once per Row.
var htmlStr = '';
for (var i = 0; i < num ; i = i + NUMPERROW){
//htmlStr += '<tr>' + cell + cell + cell + '<tr>';
if(num - i >= NUMPERROW){
htmlStr += newRow(NUMPERROW);
}else{// less than 3 to display
htmlStr += newRow(num - i);
}
}
document.getElementById('thumbnails').innerHTML = htmlStr;
}
/*
* Returns the html for a new row.
* numToAdd: the number of cells to add for this row.
*/
function newRow(cellsToAdd){
}