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

js合并单元格
<table   id="mytable"   width="14%"   border="1"   cellspacing="0"   cellpadding="0">         
<tr>               
 <td   width="40%">A</td>             
 <td   width="60%">1</td>         
</tr>         
<tr>               
 <td   width="40%">A</td>             
 <td   width="60%">2</td>         
</tr>         
<tr>               
 <td   width="40%">B</td>             
 <td   width="60%">1</td>         
</tr>         
<tr>               
 <td   width="40%">B</td>             
 <td   width="60%">1</td>         
</tr>         
<tr>               
 <td   width="40%">B</td>             
 <td   width="60%">3</td>        
</tr>
<tr>
 <td   width="40%">A</td>             
 <td   width="60%">1</td>         
</tr>     
</table>     
<input   type="button"   value="merge"   onclick="mergeCell()">         
<script   language="javascript">     
function mergeCell()     
{         
 var rows=mytable.rows;      
 var nrow=0,nlastrow;      
 var ncol=0;//for first column only,but you can use a loop here,if you want         
 while(nrow <rows.length)
 {          
  nlastrow=nrow++;       
  while(nrow<rows.length && rows[nlastrow].cells[ncol].innerText==rows[nrow].cells[ncol].innerText)
  {
   nrow++;    
   if(nrow-nlastrow>1)       
   {        
     for(var i=nlastrow+1;i<nrow;i++)        
         rows[i].deleteCell(ncol);        
   }         
   rows[nlastrow].cells[ncol].rowSpan=nrow-nlastrow;       
  }        
 }     
}  
</script>

?

?

另一个函数

?

function SpanGrid(tabObj,colIndex)

{

 if(tabObj != null)

 {

  var i,j;

  var intSpan;

  var strTemp;

  for(i = 0; i < tabObj.rows.length; i++)

  {

   intSpan = 1;

   strTemp = tabObj.rows[i].cells[colIndex].innerText;

   for(j = i + 1; j < tabObj.rows.length; j++)

   {

    if(strTemp == tabObj.rows[j].cells[colIndex].innerText)

    {

     intSpan++;

     tabObj.rows[i].cells[colIndex].rowSpan  = intSpan;

     tabObj.rows[j].cells[colIndex].style.display = "none";

    }

    else

    {

     break;

    }

   }

   i = j - 1;

  }

 }

}

?