jquery – 制作第一行的固定标题

前端之家收集整理的这篇文章主要介绍了jquery – 制作第一行的固定标题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Demo
  1. <div id="divScroll" style="overflow-x: hidden">
  2. <table id="tableAppointment" bgcolor="#fcfcfc" border="1" cellspacing="1" width="100%">
  3.  
  4. <tr>
  5. <td class="csstextheader" width="70px"></td>
  6. <td class="csstextheader" width="70px">
  7. <b>Time Slot&nbsp;</b>
  8. </td>
  9. <td><b>Room 7</b></td>
  10. <td><b>Room 8</b></td>
  11. <td><b>Room 9</b></td>
  12. <td><b>Room 10</b></td>
  13. </tr>
  14.  
  15.  
  16.  
  17. <tr class="csstablelisttd">
  18. <td width="70px">08:00AM</td>
  19. <td>00</td>
  20. <td class="csstdred">John</td>
  21. <td>&nbsp;</td>
  22. <td>&nbsp;</td>
  23. <td>&nbsp;</td>
  24. </tr>
  25. </table>
  26. </div>

在窗口上调用onload

  1. function resolutionIndependent()
  2. {
  3. var height;
  4. var tableMain;
  5. var divScroll;
  6.  
  7. if (document.body && document.body.offsetWidth)
  8. {
  9. height = document.body.offsetHeight;
  10. }
  11. if (document.compatMode == 'CSS1Compat' && document.documentElement && document.documentElement.offsetWidth)
  12. {
  13. height = document.documentElement.offsetHeight;
  14. }
  15. if (window.innerWidth && window.innerHeight)
  16. {
  17. height = window.innerHeight;
  18. }
  19. tableMain = document.getElementById('tableMain');
  20. divScroll = document.getElementById('divScroll');
  21. tableMain.style.height = parseFloat(height - 170) + 'px';
  22. divScroll.style.height = parseFloat(height - 170) + 'px';
  23. divScroll.style.overflow = "auto";
  24. }
  25. //***tableMain which is outer table of divScroll***

我必须制作第一行的固定标题..
我将style =“position:fixed”添加到第一行的每个单元格.但没有得到输出
..怎么可能

解决方法

嘿,你甚至可以使用一个简单的jquery函数,它可以在不改变你的HTML代码的情况下为你提供结果
  1. var tdWidth = $("#tableAppointment tbody tr td").width();
  2. $("#tableAppointment thead").css({'position': 'fixed','top': '0','left': '-0.2%','background-color': '#f94d4d','color': '#FFFFFF','text-align': 'center','width': '100%'});
  3.  
  4. $("#tableAppointment thead tr th").width(tdWidth);

工作示例是jsfiddle

猜你在找的jQuery相关文章