日期:2014-05-17  浏览次数:20465 次

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";-->