栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

具有固定标题的HTML表?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

具有固定标题的HTML表?

一段时间以来,我一直在寻找解决方案,但发现大多数答案都不起作用或不适合我的情况,因此我用jQuery写了一个简单的解决方案。

这是解决方案概述:

  1. 克隆需要具有固定标题的表,然后将克隆的副本放在原始副本的顶部。
  2. 从顶部桌子上取下桌子主体。
  3. 从底部表格中删除表格标题。
  4. 调整列宽。(我们会跟踪原始列宽)

以下是可运行演示中的代码。

function scrolify(tblAsJQueryObject, height) {  var oTbl = tblAsJQueryObject;  // for very large tables you can remove the four lines below  // and wrap the table with <div> in the mark-up and assign  // height and overflow property  var oTblDiv = $("<div/>");  oTblDiv.css('height', height);  oTblDiv.css('overflow', 'scroll');  oTbl.wrap(oTblDiv);  // save original width  oTbl.attr("data-item-original-width", oTbl.width());  oTbl.find('thead tr td').each(function() {    $(this).attr("data-item-original-width", $(this).width());  });  oTbl.find('tbody tr:eq(0) td').each(function() {    $(this).attr("data-item-original-width", $(this).width());  });  // clone the original table  var newTbl = oTbl.clone();  // remove table header from original table  oTbl.find('thead tr').remove();  // remove table body from new table  newTbl.find('tbody tr').remove();  oTbl.parent().parent().prepend(newTbl);  newTbl.wrap("<div/>");  // replace ORIGINAL COLUMN width  newTbl.width(newTbl.attr('data-item-original-width'));  newTbl.find('thead tr td').each(function() {    $(this).width($(this).attr("data-item-original-width"));  });  oTbl.width(oTbl.attr('data-item-original-width'));  oTbl.find('tbody tr:eq(0) td').each(function() {    $(this).width($(this).attr("data-item-original-width"));  });}$(document).ready(function() {  scrolify($('#tblNeedsScrolling'), 160); // 160 is height});<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script><div >  <table border="1" width="100%" id="tblNeedsScrolling">    <thead>      <tr><th>Header 1</th><th>Header 2</th></tr>    </thead>    <tbody>      <tr><td>row 1, cell 1</td><td>row 1, cell 2</td></tr>      <tr><td>row 2, cell 1</td><td>row 2, cell 2</td></tr>      <tr><td>row 3, cell 1</td><td>row 3, cell 2</td></tr>      <tr><td>row 4, cell 1</td><td>row 4, cell 2</td></tr>      <tr><td>row 5, cell 1</td><td>row 5, cell 2</td></tr>      <tr><td>row 6, cell 1</td><td>row 6, cell 2</td></tr>      <tr><td>row 7, cell 1</td><td>row 7, cell 2</td></tr>      <tr><td>row 8, cell 1</td><td>row 8, cell 2</td></tr>    </tbody>  </table></div>

此解决方案可在Chrome和IE中使用。由于它基于jQuery,因此它也应该在其他受jQuery支持的浏览器中也可以使用。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/614648.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号