如何在JavaScript中创建表格 [英] How to create table in javascript

查看:66
本文介绍了如何在JavaScript中创建表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用javascript创建表,想创建表document.createElement()方法.请发送示例代码.

How to create table using javascript,Iwant to create table document.createElement() method.please send sample code.

推荐答案

使用 Google搜索结果
use generate HTML table with javascript
you can also try Google Results


您可以使用以下代码

you can use below code

    var tr, td, i, oneRecord;
    var myDiv = document.createElement(''DIV'');
    myTable = document.createElement(''TABLE'');
    myTable.setAttribute("border", 1);
    // node tree
    var data = node.getElementsByTagName("NewDataSet")[0];
    for (i = 0; i < data.childNodes.length; i++) {
        // use only 1st level element nodes to skip 1st level text nodes in NN
        if (data.childNodes[i].nodeType == 1) {
            // one final match record
             öneRecord = data.childNodes[i];
            tr = myTable.insertRow(myTable.rows.length);
            td = tr.insertCell(tr.cells.length);
            td.innerHTML = oneRecord.getElementsByTagName("Name")[0].firstChild.nodeValue;
            td = tr.insertCell(tr.cells.length);
            td.innerHTML = oneRecord.getElementsByTagName("Age")[0].firstChild.nodeValue;
            td = tr.insertCell(tr.cells.length);
            td.innerHTML = oneRecord.getElementsByTagName("Org")[0].firstChild.nodeValue;
        }
    }
    myDiv.appendChild(myTable);
    return myDiv.innerHTML;
}


我知道是否使用免费的jQuery库是最好的方法之一.您可以使用描述符和诸如.after().append().appendTo.before().insertAfter().insertBefore()等的操作方法来执行此操作.

参见:
http://api.jquery.com/category/manipulation/ [ http://api.jquery.com/category/traversing/ [ http://api.jquery.com/category/selectors/ [ http://docs.jquery.com/Tutorials [
One of the best ways I know if using the freely available jQuery library. You can do it using descriptors and manipulation method like .after(), .append(), .appendTo, .before(), .insertAfter(), .insertBefore(), etc.

See:
http://api.jquery.com/category/manipulation/[^],
http://api.jquery.com/category/traversing/[^],
http://api.jquery.com/category/selectors/[^].

To start with, see:
http://docs.jquery.com/Tutorials[^].

This is really easy, the library is very compact and freely downloadable (see above).

—SA


这篇关于如何在JavaScript中创建表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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