我有一个空表定义
<table align="center" class="bordered" border="6" id="tableb_copy"></table>
我有一个定义了INPUT元素的表
<table align="center" class="bordered" border="6" id="tableb">
<tr>
<td><input type="text" value="test"></td>
<td>test1</td>
<td>test1</td>
</tr>
</table>
我使用以下代码将tableb的内容复制到tableb_copy
$('#tableb_copy').html($('#tableb').html());
它复制表结构,但如果用户在INPUT单元格中输入了一些数据,则不会复制。 有没有办法复制内容?
由于
答案 0 :(得分:1)
除了您的<table>
不符合HTML格式外,它似乎有效。
<tr>
是一个块标记,例如,您以</tr>
结束。
HTML:
<table align="center" class="bordered" border="6" id="tableb_copy"></table>
<table align="center" class="bordered" border="6" id="tableb">
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
</table>
JS:
$('#tableb_copy').html($('#tableb').html());
答案 1 :(得分:1)
首先尝试克隆表格,然后将克隆移动到新位置。
<div id='here'></div>
和js
$('#here').html($('#tableb').clone().attr('id', 'tableb_copy'));