【发布时间】:2021-08-31 02:48:57
【问题描述】:
我正在尝试找到一种将 html 元素标签插入到表结构中的方法。 html 页面中通常有多个表,因此我为每个工作正常的表动态创建并插入一个 ID。但是,当我尝试将特定的 html 元素插入表格的不同部分时,我只会得到错误,或者表格永远不会更新。
我是一名技术作家,正在尝试使用 javascript,而我使用的创作工具不允许使用 thead 和 tbody 标签,因此当我在文档中的 html 页面中遇到不具有此结构的表格时,我需要添加它。之后,我在表格中动态实现了可视化元素,以便于查找信息。
这一切在已经具有thead结构的表上都可以正常工作,因此我需要一种方法将这种结构放入没有它的表中。
这是我正在尝试做的事情的前后视图:
之前:
<table class="TableHeadingOnTop" style="border-collapse: separate"
cellspacing="0" border="1">
<tr class="t1Row">
<td class="t1Col"><p class="MoreList"><a
href="DefaultProbeComponents.htm">DefaultProbeComponents</a></p></td>
<td class="t2Col"><p class="MoreList"><a href="ProbeQualVisionOpticsCalFocusLatencyDuration1.htm">ProbeQualVisionOpticsCalFocusLatencyDuration1</a></p></td>
<td class="last"><p class="MoreList"><a
href="VFTScan_LSPX1C_Speed1.htm">VFTScan_LSPX1C_Speed1</a></p></td>
</tr>
<tr class="t2Row">
td class="t1Col"><p class="MoreList"><a href="DefMode.htm">DefMode</a></p></td>
<td class="t2Col"><p class="MoreList"><a href="ProbeQualVisionOpticsCalFocusLatencyDuration2.htm">ProbeQualVisionOpticsCalFocusLatencyDuration2</a></p></td>
<td class="last"><p class="MoreList"><a
href="VFTScan_LSPX1C_Speed2.htm">VFTScan_LSPX1C_Speed2</a></p></td>
</tr>
<tr class="t1Row">
<td class="t1Col"><p class="MoreList"><a
href="DefMoveSpeed.htm">DefMoveSpeed</a></p></td>
<td class="t2Col"><p class="MoreList"><a href="ProbeQualVisionOpticsCalFocusLatencyEnabled.htm">ProbeQualVisionOpticsCalFocusLatencyEnabled</a></p></td>
<td class="last"><p class="MoreList"><a
href="VFTScan_LSPX1H_Acceleration1.htm">VFTScan_LSPX1H_Acceleration1</a></p></td>
</tr>
</table>
之后——在这里,可以看到thead & /thead 和 tbody & /tbody 结构:
<table class="TableHeadingOnTop" style="border-collapse: separate"
cellspacing="0" border="1">
<thead>
<tr>
<th><p> </p></th>
<th><p> </p></th>
<th><p> </p></th>
</tr>
</thead>
<tbody>
<tr class="t1Row">
<td class="t1Col"><p class="MoreList"><a
href="DefaultProbeComponents.htm">DefaultProbeComponents</a></p></td>
<td class="t2Col"><p class="MoreList"><a
href="ProbeQualVisionOpticsCalFocusLatencyDuration1.htm">ProbeQualVisionOpticsCalFocusLatencyDuration1</a></p></td>
<td class="last"><p class="MoreList"><a
href="VFTScan_LSPX1C_Speed1.htm">VFTScan_LSPX1C_Speed1</a></p></td>
</tr>
<tr class="t2Row">
<td class="t1Col"><p class="MoreList"><a
href="DefMode.htm">DefMode</a></p></td>
<td class="t2Col"><p class="MoreList"><a
href="ProbeQualVisionOpticsCalFocusLatencyDuration2.htm">ProbeQualVisionOpticsCalFocusLatencyDuration2</a></p></td>
<td class="last"><p class="MoreList"><a
href="VFTScan_LSPX1C_Speed2.htm">VFTScan_LSPX1C_Speed2</a></p></td>
</tr>
<tr class="t1Row">
<td class="t1Col"><p class="MoreList"><a
href="DefMoveSpeed.htm">DefMoveSpeed</a></p></td>
<td class="t2Col"><p class="MoreList"><a
href="ProbeQualVisionOpticsCalFocusLatencyEnabled.htm">ProbeQualVisionOpticsCalFocusLatencyEnabled</a></p></td>
<td class="last"><p class="MoreList"><a
href="VFTScan_LSPX1H_Acceleration1.htm">VFTScan_LSPX1H_Acceleration1</a></p></td>
</tr>
</tbody>
</table>
如果有办法,请告诉我 - 谢谢!
以下是我面临的挑战的说明:
// In this function, I want to insert the thead structure
// and the opening and closing tbody tags...
// this function receives the value of the number of
// columns in the table and the table's ID name...
function createtheadstruct(colcount, id_name_new){
var table = document.getElementById(id_name_new);
var header = table.createTHead();
var row = header.insertRow(0);
var theadcntr = colcount-1;
var cell
// Here I'm inserting the thead data rows (td)
// based on the column counter (theadcntr)
for (var i=0; i<=theadcntr; i++) {
cell = row.insertCell(i);
cell.innerHTML = "$#160;";
}
//After the thead structure is created, I want to insert the opening tbody tag.
//Since the rows and cells of table's body are already there, I want to insert
//the opening tbody tag right after the closing thead tag (/thead) and then the
//closing tbody tag (/tbody) before the closing table tag (/table)
}```
【问题讨论】:
标签: javascript html html-table insert