简体   繁体   English

使用 jQuery 和.append 创建表

[英]Create table using jQuery and .append

I need to create table that looks like this using append jQuery.我需要使用 append jQuery 创建看起来像这样的表。 Below is my try.下面是我的尝试。

aside = $("#tabaside");
var tr = $("<tr>");
var td = $("<td>");

for (var i = 0; i < filmidata.arr.length; i++) {
  $(aside).append(tr);
  $(tr).append(td).text("text");
  $(tr).append(td).text("text");
  $(tr).append(td).text("text");
}

在此处输入图像描述

You need to create a new tr for each row, and a new td for each cell.您需要为每一行创建一个新的tr ,并为每个单元格创建一个新的td .append() doesn't make copies, it just keeps reusing the same DOM element. .append()不会复制,它只是不断重复使用相同的 DOM 元素。

You can use a nested loop to repeat the code for the cells.您可以使用嵌套循环来重复单元格的代码。

 const aside = $("#tabaside"); for (var i = 0; i < filmidata.arr.length; i++) { let tr = $("<tr>"); $(aside).append(tr); for (let j = 0; j < 3; j++) { $(tr).append($("<td>", { text: "text" })); } }

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

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