轻松搞定html表格增减

80酷酷网    80kuku.com

  <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 addRow()">增加一行</a>
<a delRow()">减去一行</a>
<table name=myTable border=1 id=myTable>
</table>



分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: