JavaScript数组到html表 [英] Javascript array to html table

查看:71
本文介绍了JavaScript数组到html表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个数组中创建一个html表。我想使用循环功能来做到这一点。但我很难找出如何将数组循环到一个html表格。我想要第一部分的国家名称和国家之上的国家。在上一节中,我希望将Capitals和Capital放在最前面。

I want to make a html table from an array. I want to use the loop function to do this. But I struggle to find out how I can loop an array to a html table. I want have the name of the countries in the first section and "country" on top of the countries. In the last section I want to have the Capitals and "Capital" on top.

这是我的html代码:

Here is my html code:

<!DOCTYPE html>
<html>
<head>
</head>
<body>


    <script>

    var country = ["Norway", "Sweden", "Denmark"];
    var capital = ["Oslo", "Stockholm" , "Copenhagen"]




    </script>
</body>
</html>


推荐答案

我为此做了一个简单的工具 - https://github.com/dszakal/ArrayToTable

I made an easy tool for this - https://github.com/dszakal/ArrayToTable

例子:

Example:

var tableGen = new ArrayToTable();
// optional
tableGen.tableclasses = 'bluetable table-with-oddeven'; // for css
tableGen.tableid = 'something-unique';

dataArray = [
{
    country: 'Norway',
    capital: 'Oslo'
}
,
{
    country: 'Sweden',
    capital: 'Stockholm'
}
,
{
    country: 'Denmark',
    capital: 'Copenhagen'
}
];

tableGen.putTableHtmlToId(dataArray, 'tableHere');

这篇关于JavaScript数组到html表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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