小编Moh*_*alo的帖子

使用 JavaScript 更新 html 方法的主体

我正在尝试使用 javascript 更新 html 表的主体。

有两种方法可以做到这一点

html表:

    <table>
     <thead>
      <tr>
        <th>Title</th>
      </tr>
     </thead>
     <tbody>
     </tbody>
    </table>
Run Code Online (Sandbox Code Playgroud)

方法一(字符串插值):

    document.querySelector('table tbody').innerHTML= 
    '<tr><td>some text</td></tr><tr><td>some text</td></tr>'

Run Code Online (Sandbox Code Playgroud)

方法二:

    const table = document.querySelector("table");
    const row = table.insertRow(0);
    const cell1 = row.insertCell(0);
    cell1.innerHTML = 'some text';
Run Code Online (Sandbox Code Playgroud)

哪种方法具有更好的性能,为什么?

假设每 1 秒我们必须更新表的整个主体,并且我们有 100 行

注意:我只想了解性能并忽略安全等其他问题

html javascript html-table

2
推荐指数
1
解决办法
470
查看次数

标签 统计

html ×1

html-table ×1

javascript ×1