jsp页面固定table头

 页面直接调用:
 

<script type="text/javascript">
jQuery(function() { 
jQuery.fn.CloneTableHeader("tab1", "div1");
});
</script>

 

  
  
  
  
  1.  
  2. jQuery.fn.CloneTableHeader = function(tableId, tableParentDivId) {  
  3.     var obj = document.getElementById("tableHeaderDiv" + tableId);  
  4.     if (obj) {  
  5.         jQuery(obj).remove();  
  6.     }  
  7.     var browserName = navigator.appName;  
  8.     var ver = navigator.appVersion;  
  9.     var browserVersion = parseFloat(ver.substring(ver.indexOf("MSIE") + 5, ver.lastIndexOf("Windows")));  
  10.     var content = document.getElementById(tableParentDivId);  
  11.     var scrollWidth = content.offsetWidth - content.clientWidth;  
  12.  
  13.     var tableOrg = jQuery("#" + tableId)  
  14.     var table = tableOrg.clone();  
  15.  
  16.     table.attr("id""cloneTable");  
  17.     var tableClone = jQuery(tableOrg).find("tr").each(function() {  
  18.  
  19.     });  
  20.     var tableHeader = jQuery(tableOrg).find("thead");  
  21.     var tableHeaderHeight = tableHeader.height();  
  22.     tableHeader.hide();  
  23.     var colsWidths = jQuery(tableOrg).find("tbody tr:first td").map(function() {  
  24.         return jQuery(this).width();  
  25.     });  
  26.     var tableCloneCols = jQuery(table).find("thead tr:first td")  
  27.     if (colsWidths.size() > 0) {  
  28.         for (i = 0; i < tableCloneCols.size(); i++) {  
  29.             if (i == tableCloneCols.size() - 1) {  
  30.                 if (browserVersion == 8.0)  
  31.                     tableCloneCols.eq(i).width(colsWidths[i] + scrollWidth);  
  32.                 else 
  33.                     tableCloneCols.eq(i).width(colsWidths[i]);  
  34.             } else {  
  35.                 tableCloneCols.eq(i).width(colsWidths[i]);  
  36.             }  
  37.         }  
  38.     }  
  39.  
  40.     var headerDiv = document.createElement("div");  
  41.     headerDiv.appendChild(table[0]);  
  42.     jQuery(headerDiv).css("height", tableHeaderHeight);  
  43.     jQuery(headerDiv).css("overflow""hidden");  
  44.     jQuery(headerDiv).css("z-index""20");  
  45.     jQuery(headerDiv).css("width""100%");  
  46.     jQuery(headerDiv).attr("id""tableHeaderDiv" + tableId);  
  47.     jQuery(headerDiv).insertBefore(tableOrg.parent());  
  
  
  
  
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <html xmlns="http://www.w3.org/1999/xhtml"> 
  3. <head runat="server"> 
  4.     <title></title> 
  5.  
  6.     <script src="jquery-1.3.2.min.js" type="text/javascript"></script> 
  7.  
  8.     <script src="CloneTableHeader.js" type="text/javascript"></script> 
  9.  
  10.     <style type="text/css"> 
  11.         .itemList  
  12.         {  
  13.             border: solid 1px #cccccc;  
  14.             overflow: hidden;  
  15.             width: 100%;  
  16.             border-collapse: collapse;  
  17.         }  
  18.         .itemList td  
  19.         {  
  20.             padding: 0px 0px 0px 0px;  
  21.             color: #444444;  
  22.             border: solid 1px #cccccc;  
  23.             text-align: center;  
  24.             line-height: 20px;  
  25.         }  
  26.         .selected  
  27.         {  
  28.             background-color: #fdee88;  
  29.         }  
  30.         .hovertr  
  31.         {  
  32.             background-color: #fff8c7;  
  33.         }  
  34.         .errortd  
  35.         {  
  36.             border: dashed 1px red;  
  37.         }  
  38.     </style> 
  39.     <script type="text/javascript"> 
  40.         jQuery(function() {  
  41.             jQuery.fn.CloneTableHeader("tab1", "div1");  
  42.         });  
  43.     </script> 
  44. </head> 
  45. <body> 
  46.     <form id="form1" runat="server"> 
  47.     <div style=" height: 100px; overflow:scroll;" id="div1"> 
  48.         <table cellpadding="0" id="tab1" cellspacing="0" border="0" class="itemList"> 
  49.             <thead> 
  50.                 <tr style="background-color: #eeeeee; margin: 0px; line-height: 20px; font-weight: bold;  
  51.                     padding: 0px 0px 0px 0px;"> 
  52.                     <td> 
  53.                         编号  
  54.                     </td> 
  55.                     <td> 
  56.                         姓名  
  57.                     </td> 
  58.                     <td> 
  59.                         年龄  
  60.                     </td> 
  61.                     <td> 
  62.                         城市  
  63.                     </td> 
  64.                 </tr> 
  65.                 <tr style="background-color: #eeeeee; margin: 0px; line-height: 20px; font-weight: bold;  
  66.                     padding: 0px 0px 0px 0px;"> 
  67.                     <td colspan="2">身高</td> 
  68.                     <td colspan="2">体重</td> 
  69.                 </tr> 
  70.             </thead> 
  71.             <tbody> 
  72.                 <tr> 
  73.                     <td> 
  74.                         1  
  75.                     </td> 
  76.                     <td> 
  77.                         张三1  
  78.                     </td> 
  79.                     <td> 
  80.                         22  
  81.                     </td> 
  82.                     <td> 
  83.                         北京  
  84.                     </td> 
  85.                 </tr> 
  86.                 <tr> 
  87.                     <td colspan="2">170</td> 
  88.                     <td colspan="2">99</td> 
  89.                 </tr> 
  90.                 <tr> 
  91.                     <td> 
  92.                         1  
  93.                     </td> 
  94.                     <td> 
  95.                         张三1  
  96.                     </td> 
  97.                     <td> 
  98.                         22  
  99.                     </td> 
  100.                     <td> 
  101.                         北京  
  102.                     </td> 
  103.                 </tr> 
  104.                 <tr> 
  105.                     <td colspan="2">170</td> 
  106.                     <td colspan="2">99</td> 
  107.                 </tr> 
  108.                 <tr> 
  109.                     <td> 
  110.                         1  
  111.                     </td> 
  112.                     <td> 
  113.                         张三1  
  114.                     </td> 
  115.                     <td> 
  116.                         22  
  117.                     </td> 
  118.                     <td> 
  119.                         北京  
  120.                     </td> 
  121.                 </tr> 
  122.                 <tr> 
  123.                     <td colspan="2">170</td> 
  124.                     <td colspan="2">99</td> 
  125.                 </tr> 
  126.                 <tr> 
  127.                     <td> 
  128.                         1  
  129.                     </td> 
  130.                     <td> 
  131.                         张三1  
  132.                     </td> 
  133.                     <td> 
  134.                         22  
  135.                     </td> 
  136.                     <td> 
  137.                         北京  
  138.                     </td> 
  139.                 </tr> 
  140.                 <tr> 
  141.                     <td colspan="2">170</td> 
  142.                     <td colspan="2">99</td> 
  143.                 </tr> 
  144.                 <tr> 
  145.                     <td> 
  146.                         1  
  147.                     </td> 
  148.                     <td> 
  149.                         张三1  
  150.                     </td> 
  151.                     <td> 
  152.                         22  
  153.                     </td> 
  154.                     <td> 
  155.                         北京  
  156.                     </td> 
  157.                 </tr> 
  158.                 <tr> 
  159.                     <td colspan="2">170</td> 
  160.                     <td colspan="2">99</td> 
  161.                 </tr> 
  162.                 <tr> 
  163.                     <td> 
  164.                         1  
  165.                     </td> 
  166.                     <td> 
  167.                         张三1  
  168.                     </td> 
  169.                     <td> 
  170.                         22  
  171.                     </td> 
  172.                     <td> 
  173.                         北京  
  174.                     </td> 
  175.                 </tr> 
  176.                 <tr> 
  177.                     <td colspan="2">170</td> 
  178.                     <td colspan="2">99</td> 
  179.                 </tr> 
  180.                 <tr> 
  181.                     <td> 
  182.                         1  
  183.                     </td> 
  184.                     <td> 
  185.                         张三1  
  186.                     </td> 
  187.                     <td> 
  188.                         22  
  189.                     </td> 
  190.                     <td> 
  191.                         北京  
  192.                     </td> 
  193.                 </tr> 
  194.                 <tr> 
  195.                     <td colspan="2">170</td> 
  196.                     <td colspan="2">99</td> 
  197.                 </tr> 
  198.                 <tr> 
  199.                     <td> 
  200.                         1  
  201.                     </td> 
  202.                     <td> 
  203.                         张三1  
  204.                     </td> 
  205.                     <td> 
  206.                         22  
  207.                     </td> 
  208.                     <td> 
  209.                         北京  
  210.                     </td> 
  211.                 </tr> 
  212.                 <tr> 
  213.                     <td colspan="2">170</td> 
  214.                     <td colspan="2">99</td> 
  215.                 </tr> 
  216.                 <tr> 
  217.                     <td> 
  218.                         1  
  219.                     </td> 
  220.                     <td> 
  221.                         张三1  
  222.                     </td> 
  223.                     <td> 
  224.                         22  
  225.                     </td> 
  226.                     <td> 
  227.                         北京  
  228.                     </td> 
  229.                 </tr> 
  230.                 <tr> 
  231.                     <td colspan="2">170</td> 
  232.                     <td colspan="2">99</td> 
  233.                 </tr> 
  234.                 <tr> 
  235.                     <td> 
  236.                         1  
  237.                     </td> 
  238.                     <td> 
  239.                         张三1  
  240.                     </td> 
  241.                     <td> 
  242.                         22  
  243.                     </td> 
  244.                     <td> 
  245.                         北京  
  246.                     </td> 
  247.                 </tr> 
  248.                 <tr> 
  249.                     <td colspan="2">170</td> 
  250.                     <td colspan="2">99</td> 
  251.                 </tr> 
  252.                 <tr> 
  253.                     <td> 
  254.                         1  
  255.                     </td> 
  256.                     <td> 
  257.                         张三1  
  258.                     </td> 
  259.                     <td> 
  260.                         22  
  261.                     </td> 
  262.                     <td> 
  263.                         北京  
  264.                     </td> 
  265.                 </tr> 
  266.                 <tr> 
  267.                     <td colspan="2">170</td> 
  268.                     <td colspan="2">99</td> 
  269.                 </tr> 
  270.             </tbody> 
  271.         </table> 
  272.     </div> 
  273.     </form> 
  274. </body> 
  275. </html> 

 

你可能感兴趣的:(jsp,table头)