简体   繁体   English

使用jQuery创建HTML表并追加

[英]Creating html table using jquery and append

I'm trying to create table based on size given by the user but as a result I get all cells in one row. 我试图根据用户给定的大小创建表,但结果我将所有单元格都放在一行中。

I've tried using loop inside loop. 我试过使用循环内循环。 One creates trs while second creates tds 一个创建trs,第二个创建tds

function createTable(size){
    for (var j=1; j<size+1; j++){
        $('table').append("<tr>");

        for (var i=1; i<size+1; i++){
            $('table').append('<td><input class="add"/></td>');

        }

        $('table').append("</tr>"); 
    }
}

I expected it to create multiple rows, not just one. 我希望它创建多个行,而不仅仅是一行。 When I open console log, all are stored in . 当我打开控制台日志时,所有内容都存储在中。

The issue is that $(selector).append('<tr>') is going to create a new DOMElement that is a TR, and append it. 问题是$(selector).append('<tr>')将创建一个新的DOMElement并添加它。 You are not appending HTML. 您没有附加HTML。 You are appending DOM Nodes. 您要附加DOM节点。 So you either have to construct the whole HTML and append it, or create the nodes, and modify them before/after you append them. 因此,您必须构造整个HTML并将其附加,或者创建节点,然后在附加它们之前/之后对其进行修改。

In this case, simply generating the HTML and then appending it is fairly straight forward. 在这种情况下,简单地生成HTML,然后附加它就很简单了。

function createTable(size){
  var newRows = '';

  for (var j=1; j<size+1; j++){
    newRows += '<tr>';

    for (var i=1; i<size+1; i++){
      newRows += '<td><input class="add"/></td>';
    }

    newRows += "</tr>";
  }

  $('table').append(newRows);
}

Make sure you add new <td> cells to <tr> elements of the table, rather than adding <td> cells to the <table> directly as you are currently doing. 确保将新的<td>单元格添加到表的<tr>元素中,而不是像当前那样直接将<td>单元格添加到<table>

Consider the following changes to resolve your problem: 请考虑以下更改以解决您的问题:

  1. create a new current "row" element as a first step of your outer loop 创建新的当前“行”元素作为外循环的第一步
  2. during your inner loop, append <td> elements to the current row 在您的内部循环中,将<td>元素附加到当前行
  3. append the current "row" to the table itself 将当前的“行”附加到表本身

This can be expressed in code as: 可以用代码表示为:

 function createTable(size) { for (var j = 1; j < size + 1; j++) { /* Create new row */ const row = $("<tr>"); for (var i = 1; i < size + 1; i++) { /* Append td cell to current row */ row.append('<td><input class="add"/></td>'); } /* Add newly created row to table */ $('table').append(row); } } createTable(10); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <table></table> 

If you store a tr as a dom object in a variable, you can append it to the table and then append to the stored tr dom object. 如果将tr作为dom对象存储在变量中,则可以将其附加到表中,然后附加到存储的trdom对象中。

 $(document).ready(function() { function createTable(size){ for (var j=1; j<size+1; j++){ var tr = $("<tr>"); $('table').append(tr); for (var i=1; i<size+1; i++){ tr.append('<td><input class="add"/></td>'); } } } createTable(5); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table></table> 

On a side note, you could control the amount of tr's and td's if you pass an array to the function. 附带说明一下,如果将数组传递给函数,则可以控制tr和td的数量。

 $(document).ready(function() { function createTable(size){ for (var j=1; j<size[0]+1; j++){ var tr = $("<tr>"); $('table').append(tr); for (var i=1; i<size[1]+1; i++){ tr.append('<td><input class="add"/></td>'); } } } createTable([3,5]); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table></table> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM