首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

js添加table行,有关问题

2012-05-05 
js添加table行,问题function AddHang() {var tb document.getElementById(mytableid)var rnum tb.r

js添加table行,问题
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里面没什么问题,但在火狐和谷歌下面都不行

[解决办法]
兼容所有浏览器的方法

HTML code
<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()">
[解决办法]
<!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 = "&nbsp;";


cell = row.insertCell(1);
cell.innerHTML = "&nbsp;";
} 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>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
<input type="button" value="添加行" onclick="AddHang()">
<input type="button" value="删除行" onclick="DelHang()">
</body>
</html>

热点排行