久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

JavaScript創(chuàng)建表格的方法

瀏覽:62日期:2023-06-21 18:29:56

本文實例為大家分享了JavaScript創(chuàng)建表格的具體代碼,供大家參考,具體內(nèi)容如下

<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>Title</title></head><body><div id = 'mountains'></div><script> let MOUNTAINS = [ {name: 'Kilimanjaro', height: 5895, place: 'Tanzania'}, {name: 'Everest', height: 8848, place: 'Nepal'}, {name: 'Mount Fuji', height: 3776, place: 'Japan'}, {name: 'Vaalserberg', height: 323, place: 'Netherlands'}, {name: 'Denali', height: 6168, place: 'United States'}, {name: 'Popocatepetl', height: 5465, place: 'Mexico'}, {name: 'Mont Blanc', height: 4808, place: 'Italy/France'} ]; // 創(chuàng)建表格 function buildTable(data) { let table = document.createElement('table'); let tr = document.createElement('tr'); // 通過 for in 循環(huán)遍歷對象,得到對象的屬性,為表頭添加內(nèi)容 for (let i in data[6]) { let th = document.createElement('th'); th.innerText = i; tr.appendChild(th); } table.appendChild(tr); // 通過 forEach 循環(huán)遍歷對象數(shù)組,為表格添加行 data.forEach((value, index) => { let tr = document.createElement('tr'); // 通過 for in 循環(huán)遍歷對象,得到對象的屬性,給每行添加內(nèi)容 for (let index1 in data[index]) { let td = document.createElement('td'); td.innerText = data[index][index1]; tr.appendChild(td); } table.appendChild(tr); }); //設置表格的對齊屬性,和邊框?qū)傩? table.setAttribute('text-align', 'right'); table.setAttribute('border','1'); return table; } document.querySelector('div').appendChild(buildTable(MOUNTAINS));</script></body></html>

JavaScript創(chuàng)建表格的方法

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標簽: JavaScript
相關(guān)文章:
主站蜘蛛池模板: 丘北县| 江安县| 贡嘎县| 通河县| 武冈市| 禄劝| 肇东市| 宁乡县| 姚安县| 满城县| 海宁市| 襄汾县| 梁平县| 宜阳县| 延庆县| 宜章县| 松阳县| 沙雅县| 息烽县| 禄劝| 凌海市| 汝州市| 瑞丽市| 壶关县| 吉安县| 金乡县| 梁河县| 从化市| 龙陵县| 体育| 黄大仙区| 徐水县| 西丰县| 台中市| 昆山市| 芮城县| 丰原市| 苗栗市| 十堰市| 吉安市| 柯坪县|