在.html()中使用for循环动态创建表 [英] Create a table dynamically using for loop in .html()

查看:3396
本文介绍了在.html()中使用for循环动态创建表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JQuery的新手,所以不知道如何继续前进。我需要使用$(this).html(..create table ...)动态创建html结构(表格)。
下面是我的伪代码。

I am new to JQuery so don't really know how to proceed ahead . I need to create an html structure (table ) dynamically using $(this).html (..create table...). Below is my pseudo code.

$(this).html('<table>for(var a=0;a<NoteCount;a++){<tr><td><div id = "NotePadTextArea">NoteArrayVal[a]</div></td></tr>}</table>'
);

NoteArrayVal是一个已经包含值的数组。
如何继续这种设计?
请帮助。

NoteArrayVal is an array which already has values in it. How to go ahead with this kind of design? Please help.

推荐答案

您必须在循环中创建td和text节点。此代码仅创建2个td,因此只有2个可见。示例:

You must create td and text nodes within loop. This code creates only 2 td, so only 2 are visible. Example:

var table = document.createElement('table');
for (var i = 1; i < 4; i++) {
    var tr = document.createElement('tr');   
    var td1 = document.createElement('td');
    var td2 = document.createElement('td');
    var text1 = document.createTextNode('Text1');
    var text2 = document.createTextNode('Text2');
    td1.appendChild(text1);
    td2.appendChild(text2);
    tr.appendChild(td1);
    tr.appendChild(td2);
    table.appendChild(tr);
}
document.body.appendChild(table);

这篇关于在.html()中使用for循环动态创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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