js添加table行,问题

xuejiyong0619 2012-05-03 03:23:06
function AddHang() {
var tb = document.getElementById("mytableid");
var rnum = tb.rows.length;
var row = tb.insertRow();
var cell = row.insertCell();
cell.innerText = rnum ;
cell = row.insertCell();
cell.innerHTML = "<input type='text' style='width:95%' id='ipttime'" + rnum + " onclick='WdatePicker();'>";
cell = row.insertCell();
cell.innerHTML = "<input type='text' style='width:95%' id='iptchu'" + rnum + ">";
cell = row.insertCell();
cell.innerHTML = "<input type='text' style='width:95%' id='iptbao'" + rnum + ">";
cell = row.insertCell();
cell.innerHTML = "<input type='text' style='width:91%' id='iptjine'" + rnum + ">";
cell = row.insertCell();
cell.innerHTML = "<input type='text' style='width:95%' id='iptdui'" + rnum + ">";
cell = row.insertCell();
cell.innerHTML = "<input type='text' style='width:95%' id='iptbei'" + rnum + ">";
cell = row.insertCell();
}我这样添加行,在ie里面没什么问题,但在火狐和谷歌下面都不行
...全文
406 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuejiyong0619 2012-05-04
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 的回复:]
function AddHang() {
var tb = document.getElementById("mytableid");
var rnum = tb.rows.length;
var row = tb.insertRow(rnum);
var cell = row.insertCell(0);
cell.innerHTML = rnum ;
cell = row.inse……
[/Quote]
终究是名人
SRC01 2012-05-04
  • 打赏
  • 举报
回复
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
<script language="javascript">
//添加行处理
function AddHang() {
try{
var tb = document.getElementById("Datatable");
var rnum = tb.rows.length;
var row = tb.insertRow(rnum);
var cell = row.insertCell(0);
<!--cell.align="center";-->
cell.innerHTML = " ";
cell = row.insertCell(1);
cell.innerHTML = " ";
} catch(e){
alert(e);
}
}
//删除行处理
function DelHang(){
try{
var tb = document.getElementById("Datatable");
var rnum = tb.rows.length;
if(rnum > 0){
tb.deleteRow(rnum - 1);
}
} catch(e){
alert(e);
}
}
</script>

</head>

<body>
<table width="500" border="1" id="Datatable">
<tr>
<td> </td>
<td> </td>
</tr>
</table>
<input type="button" value="添加行" onclick="AddHang()">
<input type="button" value="删除行" onclick="DelHang()">
</body>
</html>
孟子E章 2012-05-04
  • 打赏
  • 举报
回复
兼容所有浏览器的方法

<script>
function AddHang() {
var tb = document.getElementById("mytableid");
if (tb.tBodies.length == 0) {
tb.appendChild(document.createElement("tbody"));
}
var rnum = tb.tBodies[0].rows.length;
var row = tb.tBodies[0].insertRow(rnum);
var cell = row.insertCell(row.cells.length);
cell.innerHTML = rnum;
cell = row.insertCell(row.cells.length);
cell.innerHTML = "<input type='text' style='width:95%' id='ipttime" + rnum + "' onclick='WdatePicker();'>";
cell = row.insertCell(row.cells.length);
cell.innerHTML = "<input type='text' style='width:95%' id='iptchu" + rnum + "'>";
cell = row.insertCell(row.cells.length);
cell.innerHTML = "<input type='text' style='width:95%' id='iptbao" + rnum + "'>";
cell = row.insertCell(row.cells.length);
cell.innerHTML = "<input type='text' style='width:91%' id='iptjine" + rnum + "'>";
cell = row.insertCell(row.cells.length);
cell.innerHTML = "<input type='text' style='width:95%' id='iptdui" + rnum + "'>";
cell = row.insertCell(row.cells.length);
cell.innerHTML = "<input type='text' style='width:95%' id='iptbei" + rnum + "'>";
cell = row.insertCell(row.cells.length);
}
</script>
<table id="mytableid" style="width: 100%">
</table>
<input type="button" value="test" onclick="AddHang()">
全局变量 2012-05-04
  • 打赏
  • 举报
回复
$("#mytableid tr").empty();
var newLine = $("#mytableid").length;
var row = comm.insertRow(newLine);
var col = row.insertCell(0);
col.innerHTML = data[i].UserIntNo;
col = row.insertCell(1);
col.innerHTML = data[i].FiPaIntNo;
education520 2012-05-04
  • 打赏
  • 举报
回复
直接把<tr><td>标签写成字符串,然后插进去
一克代码 2012-05-04
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]
jquery有没事例。。
[/Quote]

就你那么简单的一个table 直接拼装就行 !

不要说你拼接一个table的字符串都不会啊 !
全局变量 2012-05-04
  • 打赏
  • 举报
回复
$("table:eq(0)").find("tr").each(function(){
$(this).append("<td>----</td>");
});
玄冰2013 2012-05-04
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]

为什么不用jquery呢?

jquery 有追加元素的方法
[/Quote]
++1
caobob 2012-05-04
  • 打赏
  • 举报
回复
jQuery有本书叫《锋利的jQuery》有例子操作Table的。非常简单。

其实,如果你的Table1不大的话,直接把html替换成新的内容也就OK了。当然,效率低下。
xuejiyong0619 2012-05-04
  • 打赏
  • 举报
回复
jquery有没事例。。
xuan.ye 2012-05-04
  • 打赏
  • 举报
回复
为什么不用jquery呢?

jquery 有追加元素的方法
一克代码 2012-05-04
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]
你那个太复杂了,有没简单一点的啊
[/Quote]

js拼装字符串还复杂啊!
xuejiyong0619 2012-05-04
  • 打赏
  • 举报
回复
你那个太复杂了,有没简单一点的啊
一克代码 2012-05-03
  • 打赏
  • 举报
回复
最好用字符串拼接的

我的博客里面有个

你可以看看
js动态生成表格 .
cpio 2012-05-03
  • 打赏
  • 举报
回复
直接用append添加一行的内容"<tr><td><input ...</td></tr>"
xuejiyong0619 2012-05-03
  • 打赏
  • 举报
回复
麻烦点,有没代码看下啊
newxdlysk 2012-05-03
  • 打赏
  • 举报
回复
貌似火狐谷歌不支持哈,用document.createElement()通用的,就是比这个麻烦点

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧