iLeichun

当前位置: 首页 > JavaScript

js实现html表格增减

分类:JavaScript   来源:网络   时间:2010-08-17 00:54:40

<script>
function addRow(){
    
    // 插入一行
    myNewRow = document.all.myTable.insertRow();
    var lenRow = document.all.myTable.rows.length; // 计算总行数
    
    if(lenRow > 1){        
        var point = myNewRow.rowIndex;    // 计算当前行位置    
        var lenCol = document.all.myTable.rows(0).cells.length; // 计算每行有几列
        
        // 插入lenCol个td
            for (i=0; i < lenCol; i++) {
                document.all.myTable.rows(point).insertCell();
                document.all.myTable.rows(point).cells(i).innerHTML = parseFloat(document.all.myTable.rows(point-1).cells(i).innerHTML) + lenCol;
        }
    }else if(lenRow == 1){
        // 原来table是0行,初始化一行
        for(i = 0; i < 10; i++){
            document.all.myTable.rows(0).insertCell();
            document.all.myTable.rows(0).cells(i).innerHTML = i;
        }
    }    
}

function delRow(){
    document.all.myTable.deleteRow();
}
</script>
<a href="javascript: addRow()">增加一行</a>
<a href="javascript: delRow()">减去一行</a>
<table name=myTable border=1 id=myTable>
</table>
 

更多