Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JavaScript创建表_Javascript_Arrays_Dom_Html Table - Fatal编程技术网

使用JavaScript创建表

使用JavaScript创建表,javascript,arrays,dom,html-table,Javascript,Arrays,Dom,Html Table,我应该使用JavaScript从头开始创建这样的表,但我不知道如何创建 我应该使用数组吗 我对JavaScript还是新手,这是我学习JavaScript的第一周。谢谢你的帮助 BMI Health category Below 18.5 Under weight Between 18.5 and 23 Normal weight Between 23 and 27.5 Over weight Above 27.5

我应该使用JavaScript从头开始创建这样的表,但我不知道如何创建

我应该使用数组吗

我对JavaScript还是新手,这是我学习JavaScript的第一周。谢谢你的帮助

BMI                   Health category

Below 18.5            Under weight

Between 18.5 and 23   Normal weight

Between 23 and 27.5   Over weight

Above 27.5            Obese

用纯JS创建表:

var table_header=['BMI','Health category'];
变量表_行=[
[‘低于18.5’、‘体重不足’、[‘介于18.5和23之间’、‘正常体重’],
[‘23至27.5岁’、‘超重’、[‘27.5岁以上’、‘肥胖’]
];
var dom_body=document.getElementsByTagName('body')[0];
var table=document.createElement('table');
var tbody=document.createElement('tbody');
//使用for循环将行和单元格添加到表体
var tr=document.createElement('tr');
表_header.forEach(函数(值、索引){
var td=document.createElement('td');//表格单元格
td.innerHTML=value;//设置单元格值
tr.appendChild(td);//将其添加到行中
})
t附肢儿童(tr)//添加表格标题
//插入表行
表\u rows.forEach(函数(行、索引){
var tr=document.createElement('tr');
//为当前行追加单元格
row.forEach(函数(值、索引){
var td=document.createElement('td');//表格单元格
td.innerHTML=value;//设置单元格值
tr.appendChild(td);//将其添加到行中
})
tbody.appendChild(tr);//添加行
})
//在dom中添加表
表3.儿童(t身体);
dom_body.appendChild(表)