Javascript 使用循环引导添加行
我想做两列我知道结构是这样的Javascript 使用循环引导添加行,javascript,jquery,twitter-bootstrap,pug,Javascript,Jquery,Twitter Bootstrap,Pug,我想做两列我知道结构是这样的 .row .col-md-6 .col-md-6 .row .col-md-6 .col-md-6 但是如何在javascript中使用循环来编写它呢 - each obj,index in obj if (index > 0) .row .col-md-6 这看起来很直截了当。您必须为循环创建一个嵌套的,该循环使用classrow和classcol-
.row
.col-md-6
.col-md-6
.row
.col-md-6
.col-md-6
但是如何在javascript中使用循环来编写它呢
- each obj,index in obj
if (index > 0)
.row
.col-md-6
这看起来很直截了当。您必须为循环创建一个嵌套的,该循环使用class
row
和classcol-md-6
创建x
div和y
div
var x = 2,
y = 2,
colClass = 'col-md-6',
rowClass = 'row',
rows = [],
i, j, currentRow, currentCol;
// Variable x is the amount of rows to create.
for (i = 0; i < x; i++) {
// Create the row.
currentRow = document.createElement('div');
currentRow.classList.add(rowClass);
// Variable y is the amount of columns to create for each row.
for (j = 0; j < y; j++) {
// Create the column.
currentCol = document.createElement('div');
currentCol.classList.add(colClass);
// Append the column to the row.
currentRow.appendChild(currentCol);
}
// Add the row to an array of rows.
rows.push(currentRow);
}
// Append the rows to something, or do something else with them.
// Below practice could already be done inside the for loop, instead of rows.push(currentRow)
rows.forEach(function (row) {
document.body.appendChild(row);
});
var x=2,
y=2,
colClass='col-md-6',
rowClass='行',
行=[],
i、 j,currentRow,currentCol;
//变量x是要创建的行数。
对于(i=0;i
addforloop为“col-md-6”创建2列,或者您需要其他内容?为什么要添加新的“.row”?大于12列单位的行将被简单地删除。您使用的是Jade还是其他什么?这不是正确的Javascript语法