问题:我必须动态显示单元格,每行必须有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){
}
答案 0 :(得分:1)
如果我正确理解了您的问题,您可以使用
获取需要添加的单元格数量newRow = NUMPERROW - (num % NUMPERROW)
其中%是模运算符:7%3是7/3的提醒,即1