JavaScript 关于动态添加表格行问题





strHtml="<table border=1 id=McGrid1>"
strHtml=strHtml + "<tr><td align='center' style='display:none'>编号</td><td align='center' width='60'>姓名</td</tr></table>"
document.write(strHtml)

tr=document.all.McGrid1.insertRow()
td=tr.insertCell()
//tr.insertCell(0).style.display="none"
//tr.insertCell(0).innerText="012"
td.style.display="none"
td.innerText="012"
td=tr.insertCell()
td.innerText="张三"

//tr.insertCell(1).innerText="张三"
alert(tr.cells(1).innerText);

=========================================

rows

Every table contains a rows collection, which consists of all the rows in a table, including the header and footer rows. You can use the insertRow method to add a row to a table and, consequently, to its rows collection. Each row is equivalent to a tr element in HTML. The following code appends an empty row to the rows collection of a table called oTable.

var oRow
oRow = oTable.insertRow();

By default, the insertRow method appends a row to the end of the collection; this is faster than inserting a row somewhere in the middle of the collection. However, to insert a row into the middle of a collection, you can pass an index to insertRow, as shown in the following code.

var oRow
oRow = oTable.insertRow(3);

The index on the rows collection starts at zero. Therefore, the preceding code inserts a row at the fourth position of the rows collection of oTable. Once a row is created, you can insert cells into it. This is discussed in the next section.

Note   The innerText and innerHTML properties are read-only on the tr object. Therefore, you cannot use them to insert text or HTML into a row. You can, however, insert td objects into a row using the insertCell method. Then, you can insert text or HTML into the td objects by setting the innerText or innerHTML property of the cell.

You can access the members of the rows collection by using an index, the same way you access an array. Consider the following code, which iterates through the rows of oTable and sets the font to bold for each row. The code uses array syntax to access the individual members of the rows collection. The length property is used to determine the number of elements in a collection.

var curr_row;

for (curr_row = 0; curr_row < oTable.rows.length; curr_row++)
{
  oTable.rows[curr_row].style.fontWeight = "bold";
}

cells

After a row is created, you can add cells to it; each row contains a cells collection. To append cells to the cells collection of a row object, call the insertCell method of the row object.

Note   By default, the insertCell method adds cells to the end of the collection, because it is faster to add a cell at the end of a row than somewhere in the middle.

The following example inserts a row into a table, and then it inserts a cell into the newly inserted row.

var oRow;
var oCell;

oRow = oTable.insertRow();
oCell = oRow.insertCell();

Once an empty cell is added to a row, you can insert HTML into the cell by setting its innerHTML property.

var oRow;
var oCell;

oRow = oTable.insertRow();
oCell = oRow.insertCell();
oCell.innerHTML = "Download the latest version of <I>Internet Explorer</I> from here."

oRow.insertCell(3) 不是第3个位置的表格而是在第3个位置插入的表格!



posted @ 2005-03-10 10:02  PointNet  阅读(1763)  评论(0编辑  收藏  举报