【问题标题】:How to set dynamic id to a table and append it to a div?如何将动态 id 设置为表并将其附加到 div?
【发布时间】:2021-07-22 05:27:48
【问题描述】:

我想动态创建 10 个表并将其附加到具有 table_container 类的 div 容器中。 这是我正在尝试的 sn-p,我需要根据我的要求对其进行任何更改吗?

for( let i =0; i< 10; i++){
  var table = document.createElement('table');
  table.setAttribute("id", "table"+i);
  }
<div class="table_container">
    
</div>

【问题讨论】:

标签: javascript html jquery typescript


【解决方案1】:
// Get a reference to the container.
const container = document.querySelectorAll('.table_container')[0];

for(let i = 0; i < 10; i++){
  // Create the new <table>
  const table = document.createElement('table');
  table.setAttribute("id", "table"+i);
  
  // Append it to the inside of the container.
  container.appendChild(table);
}

【讨论】:

    猜你喜欢
    • 2011-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-06
    • 2015-12-20
    • 1970-01-01
    • 2012-05-13
    相关资源
    最近更新 更多