简体   繁体   中英

Create table using jQuery and .append

I need to create table that looks like this using 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. .append() doesn't make copies, it just keeps reusing the same DOM element.

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" })); } }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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