table表格單元格合併程式碼例項

螞蟻小編發表於2017-02-26
表格一般是用來組織資料的,有些表格中的資料合併起來更容易閱讀。

程式碼如下:

[HTML] 純文字檢視 複製程式碼
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>表格單元格合併程式碼例項-螞蟻部落</title>
<style type="text/css">
body,td,th {
  font-family:Verdana, Arial, Helvetica, sans-serif;
  font-size:12px;
}
#gridview, #gridview td {
  border:1px #ccc solid;
  border-collapse:collapse;
}
</style>
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  var grid=$("#gridview"); 
  var rowCount=grid.find("tr").length-1; 
  var flagRow=grid.find("tr:eq("+rowCount+")").find("td:eq(0)").html(); 
  var sp=0; 
  for(var i=rowCount;i>=0;i--) { 
    var tempRow=grid.find("tr:eq("+i+")"); 
    if(flagRow==tempRow.find("td:eq(0)").html()) { 
      tempRow.find("td:eq(0)").remove(); 
    } 
    else { 
      $("<td/>").attr("rowspan",rowCount-i-sp)
      .html(flagRow)
      .prependTo(tempRow.parent()
      .find("tr:eq("+(i+1)+")")); 
      flagRow=tempRow.find("td:eq(0)").html(); 
      sp=rowCount-i; 
      if(i!=0) { 
        tempRow.find("td:eq(0)").remove(); 
      } 
    } 
  }
})  
</script>
</head>
<body>
<table id="gridview" width="386" border="0" cellspacing="0" cellpadding="6">
  <tr>
    <td width="59" align="center"><strong>課別</strong></td>
    <td width="76" align="center"><strong>班別</strong></td>
    <td width="73" align="center"><strong>班次</strong></td>
    <td width="130" align="center"><strong>出勤時間</strong></td>
  </tr>
  <tr>
    <td>陳偉勳</td>
    <td>一班制</td>
    <td>白班</td>
    <td>2011-09-09</td>
  </tr>
  <tr>
    <td>陳偉勳</td>
    <td>一班制</td>
    <td>夜班</td>
    <td>2011-09-09</td>
  </tr>
  <tr>
    <td>陳偉勳</td>
    <td>兩班制</td>
    <td>白班</td>
    <td>2011-09-09</td>
  </tr>
  <tr>
    <td>陳偉勳</td>
    <td>兩班制</td>
    <td>白班</td>
    <td>2011-09-09</td>
  </tr>
  <tr>
    <td>陳偉勳</td>
    <td>兩班制</td>
    <td>夜班</td>
    <td>2011-09-09</td>
  </tr>
  <tr>
    <td>陳偉勳</td>
    <td>三班制</td>
    <td>早班</td>
    <td>2011-09-09</td>
  </tr>
  <tr>
    <td>陳偉勳</td>
    <td>三班制</td>
    <td>中班</td>
    <td>2011-09-09</td>
  </tr>
  <tr>
    <td>陳偉勳</td>
    <td>三班制</td>
    <td>晚班</td>
    <td>2011-09-09</td>
  </tr>
  <tr>
    <td>張文軍</td>
    <td>一班制</td>
    <td>白班</td>
    <td>2011-09-09</td>
  </tr>
  <tr>
    <td>張文軍</td>
    <td>兩班制</td>
    <td>白班</td>
    <td>2011-09-09</td>
  </tr>
  <tr>
    <td>張文軍</td>
    <td>兩班制</td>
    <td>白班</td>
    <td>2011-09-09</td>
  </tr>
  <tr>
    <td>張文軍</td>
    <td>兩班制</td>
    <td>夜班</td>
    <td>2011-09-09</td>
  </tr>
  <tr>
    <td>張文軍</td>
    <td>三班制</td>
    <td>夜班</td>
    <td>2011-09-09</td>
  </tr>
  <tr>
    <td>張文軍</td>
    <td>三班制</td>
    <td>中班</td>
    <td>2011-09-09</td>
  </tr>
</table>
</body>
</html>

以上程式碼實現了我們想要的功能,能夠將第一列中內容相同的單元格進行合併。

相關文章