通过js向table添加或修改row及cell

来源:互联网 发布:百安居建材 知乎 编辑:程序博客网 时间:2024/04/30 23:48
  1. //取得table对象   
  2.         var tabObj = document.getElementById("tabTipResult");   
  3.     //目前table有多少行   
  4.         var rowCount = tabObj.rows.length;   
  5.     //每行有多少列   
  6.         var cellCount = tabObj.rows(0).cells.length;   
  7.   
  8.         if(rowCount < pageCount)   
  9.         {   
  10.             for(var i=0;i< pageCount ;i++)   
  11.             {   
  12.         //循环 向table增加新行   
  13.                 var newRow = tabObj.insertRow(rowCount++);     
  14.                 for(var j=0;j<cellCount;j++)             
  15.                 {   
  16.             //每行的新列值       
  17.                     newRow.insertCell(j).innerHTML="无";               
  18.                 }   
  19.             }   
  20.         }   
  21.         else  
  22.         {   
  23.             for(var i=1;i<= pageCount ;i++)   
  24.             {   
  25.                 var tmpRow = tabObj.rows(i);     
  26.                 for(var j=0;j<cellCount;j++)             
  27.                 {   
  28.                     tmpRow.cells(j).innerHTML="无";               
  29.                 }   
  30.         //该行的背景色属性   
  31.         tmpRow .onmouseout = function(){this.bgColor="white";};   
  32.         tmpRow .onmouseover= function(){this.bgColor="#efffff";};   
  33.         //td 在 onclick事件   
  34.         tmpRow .cells(0).onclick = function(){ alert("");};      
  35.             }   
  36.         }   
  37.          //删除列   
  38.       document.getElementById(tabTipResult).deleteRow(index);