<head>
<title>Sample code - Traversing an HTML Table with JavaScript and DOM Interfaces</title>
<script>
function start() {
// get the reference for the body
var mybody=document.getElementsByTagName("body").item(0);
// creates an element whose tag name is TABLE
mytable = document.createElement("TABLE");
// creates an element whose tag name is TBODY
mytablebody = document.createElement("TBODY");
// creating all cells
for(j=0;j<2;j++) {
// creates an element whose tag name is TR
mycurrent_row=document.createElement("TR");
for(i=0;i<2;i++) {
// creates an element whose tag name is TD
mycurrent_cell=document.createElement("TD");
// creates a Text Node
currenttext=document.createTextNode("cell is row "+j+", column "+i);
// appends the Text Node we created into the cell TD
mycurrent_cell.appendChild(currenttext);
// appends the cell TD into the row TR
mycurrent_row.appendChild(mycurrent_cell);
}
// appends the row TR into TBODY
mytablebody.appendChild(mycurrent_row);
}
// appends TBODY into TABLE
mytable.appendChild(mytablebody);
// appends TABLE into BODY
mybody.appendChild(mytable);
// sets the border attribute of mytable to 2;
mytable.setAttribute("border","2");
}
</script>
</head>
<body onload="start()">
</body>
</html>
<title>Sample code - Traversing an HTML Table with JavaScript and DOM Interfaces</title>
<script>
function start() {
// get the reference for the body
var mybody=document.getElementsByTagName("body").item(0);
// creates an element whose tag name is TABLE
mytable = document.createElement("TABLE");
// creates an element whose tag name is TBODY
mytablebody = document.createElement("TBODY");
// creating all cells
for(j=0;j<2;j++) {
// creates an element whose tag name is TR
mycurrent_row=document.createElement("TR");
for(i=0;i<2;i++) {
// creates an element whose tag name is TD
mycurrent_cell=document.createElement("TD");
// creates a Text Node
currenttext=document.createTextNode("cell is row "+j+", column "+i);
// appends the Text Node we created into the cell TD
mycurrent_cell.appendChild(currenttext);
// appends the cell TD into the row TR
mycurrent_row.appendChild(mycurrent_cell);
}
// appends the row TR into TBODY
mytablebody.appendChild(mycurrent_row);
}
// appends TBODY into TABLE
mytable.appendChild(mytablebody);
// appends TABLE into BODY
mybody.appendChild(mytable);
// sets the border attribute of mytable to 2;
mytable.setAttribute("border","2");
}
</script>
</head>
<body onload="start()">
</body>
</html>
var cell;
for (var i=0; i < 5; i++) {
row = tblUpdate.insertRow();//tblUpdate是某个表的ID
for (var j=0; j<5; j++) {
cell = row.insertCell();
cell.innerText = "第" + i + "行,第" + j + "单元格";
}
}
function addRow() {
var newCell;
alert(1);
var newRow=document.all.table1.insertRow(0);
alert(2);
for(var i=0; i<table1.rows(1).cells.length; i++) {
newCell=newRow.insertCell(i);
newCell.innerText="newCell";
}
}</script>
<table id="table1" border="1">
<tr><td>
1
</td></tr>
</table>
<input type=button onclick="addRow()">
function addRowFunction() {
var newRow=document.all.table1.insertRow();
for(var i=0; i<document.all.table1.rows[0].cells.length; i++) {
alert(1);
var newCell=newRow.insertCell[i];
alert(2);
newCell.innerText="newCell";
alert(3);
}
}
该函数执行到
newCell.innerText="newCell";
那行就报错了,请高手帮忙解释一下