我有一个包含一些复杂值的表(我的意思是,我必须发出几个请求才能获得我想要的所有值)。我需要在此表的末尾添加行,并使用与现有html代码相同的代码填充单元格。
例如,我有这样的东西:
<table id="table_cultures">
<tr>
<td>Just a string</td>
<td>Dropdown</td>
<td><div class='foo'> something..</div></td>
</tr>
</table>
<button id="plus_button" onclick="addRow()">+</button>现在,在我的javascript中,我有类似这样的东西:
function addRow(){
var table = document.getElementById("table_cultures"); //Get the table element
var itk = document.getElementById('foo'); //Try to get the html to duplicate
var row = table.insertRow(-1); //Add in the end of the table
var c1 = row.insertCell(0);
var c2 = row.insertCell(1);
var c3 = row.insertCell(2);
c1.innerHTML= 'Culture';
c2.innerHTML='test';
c3.innerHTML=itk;
}因此,在变量itk中,我试图生成html并将其放入单元格,它只显示HTMLFormElement (因为它是我想要复制的表)。
这样做有可能吗?或者还有一种更简单的方法?
发布于 2016-05-04 16:35:56
您应该考虑克隆行节点,而不是获取并设置innerHTML,如下所示:
var table = document.getElementById("table_cultures"); //Get the table element
var row = document.getElementById("rowTobeCloned"); //clone the required row
var clone = row.cloneNode(true); // copy child nodes too
table.appendChild(clone); // append the new row to the end of the tablehttps://stackoverflow.com/questions/37022340
复制相似问题