日期:2014-05-16  浏览次数:20357 次

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>