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

js 怎么增加列

2012-02-11 
js 如何增加列HTML codehtmlhead/headbodytable idmyTable border1trtdRow1 cell1/td

js 如何增加列

HTML code
<html>    <head>    </head>    <body>        <table id="myTable" border=1>            <tr>                <td>                    Row1 cell1                </td>                <td>                    Row1 cell2                </td>            </tr>            <tr>                <td>                    Row2 cell1                </td>                <td>                    Row2 cell2                </td>            </tr>            <tr>                <td>                    Row3 cell1                </td>                <td>                    Row3 cell2                </td>            </tr>        </table>        <script>function insRow(){var x=document.getElementById('myTable').insertRow(0)var y=x.insertCell(0)var z=x.insertCell(1)y.innerHTML="NEW CELL1"z.innerHTML="NEW CELL2"    }    </script>        <input type="button" value="InsertRow" onclick="insRow()";>    </body></html>

----------
这是增加一行的代码,如何增加一列呢?谢谢!

[解决办法]
function insCell(){
var x=document.getElementById('myTable').rows[0];
var y=x.insertCell(0)
y.innerHTML="NEW CELL1"
}
在第一行插入一列,要每行插循环一下
[解决办法]
L@_@K
HTML code
<html>    <head>    </head>    <body>        <table id="myTable" border=1>            <tr>                <td>                    Row 0, cell 0                </td>                <td>                    Row 0, cell 1                </td>            </tr>            <tr>                <td>                    Row 1, cell 0                </td>                <td>                    Row 1, cell 1                </td>            </tr>            <tr>                <td>                    Row 2, cell 0                </td>                <td>                    Row 2, cell 1                </td>            </tr>        </table>        <script>function insRow(iIndex){    var t = document.getElementById('myTable');    var columnCount = t.rows[0].cells.length;    var r = t.insertRow(iIndex);    var c;    for (var i=0; i<columnCount; i++)    {        c = r.insertCell(i);        c.innerText = "Row " + iIndex + ", cell " + i;    }}function appCol(){    var t=document.getElementById('myTable');    var r, c;    for (var i=0; i<t.rows.length; i++)    {        r = t.rows[i];        c = r.insertCell();        c.innerText = "Row " + i + ", cell " + (r.cells.length-1);    }}    </script>        <input type="button" value="InsertRow" onclick="insRow(0)";>        <input type="button" value="AppendColumn" onclick="appCol()";>    </body></html> 

热点排行