[英]Generate table based on number of rows, columns in jquery
如何根据给定的行数和列数在jQuery中生成表?
You can use nested for loops, create elements and append them to one another. 您可以使用嵌套for循环,创建元素并将它们相互追加。 Here's a very simple example that demonstrates creating DOM elements, and appending them. 这是一个非常简单的示例,演示了创建DOM元素并附加它们。
You'll notice that the $('<tag>')
syntax will create an element, and you can use the appendTo method to, well, do exactly that. 你会注意到$('<tag>')
语法将创建一个元素,你可以使用appendTo方法来做到这一点。 Then, you can add the resulting table to the DOM. 然后,您可以将结果表添加到DOM。
var rows = 5; //here's your number of rows and columns
var cols = 5;
var table = $('<table><tbody>');
for(var r = 0; r < rows; r++)
{
var tr = $('<tr>');
for (var c = 0; c < cols; c++)
$('<td>some value</td>').appendTo(tr); //fill in your cells with something meaningful here
tr.appendTo(table);
}
table.appendTo('body'); //Add your resulting table to the DOM, I'm using the body tag for example
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.