如何使用 jquery 创建一个包含行的新表并将其包装在 div 中 [英] How to create a new table with rows using jquery and wrap it inside div

查看:10
本文介绍了如何使用 jquery 创建一个包含行的新表并将其包装在 div 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 div 元素内创建表格

在我的 .html 文件中,我有这个

在我的 js 文件中,我想放置带有行和数据的新表

我该如何实施?

解决方案

假设您有表格的 HTML,您可以简单地从中创建一个 jQuery 对象并将其附加到 DIV.如果您有数据,则需要遍历它并从数据中创建单元格/行并独立添加它们.

$('<table><tr><td>.....</td></tr></table>').appendTo('#div1');

var data = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [7, 8, 9 ] ];var html = '<table><thead><tr>...</tr></thead><tbody>';for (var i = 0, len = data.length; i < len; ++i) {html += '';for (var j = 0, rowLen = data[i].length; j < rowLen; ++j ) {html += ''+ 数据[i][j] + '</td>';}html += "</tr>";}html += '</tbody><tfoot><tr>....</tr></tfoot></table>';$(html).appendTo('#div1');

I would like to create table inside a div element

On my .html file, I have this

<div id='div1'> </div>

On my js file, I want to place new table with rows and with data

How can I implement this?

解决方案

Assuming you have the HTML for the table, you can simply create a jQuery object out of it and append it to the DIV. If you have the data, you'll need to iterate through it and create the cells/rows from the data and add them independently.

$('<table><tr><td>.....</td></tr></table>').appendTo( '#div1' );

or

var data = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [7, 8, 9 ] ];

var html = '<table><thead><tr>...</tr></thead><tbody>';
for (var i = 0, len = data.length; i < len; ++i) {
    html += '<tr>';
    for (var j = 0, rowLen = data[i].length; j < rowLen; ++j ) {
        html += '<td>' + data[i][j] + '</td>';
    }
    html += "</tr>";
}
html += '</tbody><tfoot><tr>....</tr></tfoot></table>';

$(html).appendTo('#div1');

这篇关于如何使用 jquery 创建一个包含行的新表并将其包装在 div 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆