亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 JavaScript 在 div 中創建表格

使用 JavaScript 在 div 中創建表格

躍然一笑 2023-12-04 14:38:02
超文本標記語言       <h1>Javascript Playground</h1>        <div id="first"></div>        <div id="second"></div>    </body>我需要在 div 中插入一個包含此信息的表格,包括代碼、標題和產品列,而不接觸 HTML(僅使用 JavaScript)let units = [    {        'code': 'COMP2110',        'title': 'Web Technology',         'offering': 'S1'    },      {        'code': 'COMP2010',        'title': 'Algorithms and Data Structures',         'offering': 'S1'    },]
查看完整描述

1 回答

?
慕田峪7331174

TA貢獻1828條經驗 獲得超13個贊

也許你可以像下面這樣開始:


let units = [{'code': 'COMP2110', 'title': 'Web Technology', 'offering': 'S1'}, {'code': 'COMP2010', 'title': 'Algorithms and Data Structures', 'offering': 'S1' } ];


// find your div first

const div = document.getElementById('first');


// building the HTML for the div

let html = `<table>

     <thead>

         <tr>

           ${Object.keys(units[0]).map(e => `<th>${e}</th>`).join('')}

         </tr>

     </thead>`


html += `<tbody>`;

units.forEach(e => html += `<tr>

      ${Object.values(e).map(v => `<td>${v}</td>`).join('')}

</tr>`);


html += `</tbody></table>`;


// adding HTML to your div

div.innerHTML = html;

table, th, td {

  border: 1px solid black;

}

<h1>Javascript Playground</h1>


<div id="first"></div>

<div id="second"></div>


查看完整回答
反對 回復 2023-12-04
  • 1 回答
  • 0 關注
  • 152 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號