`

一个兼容IE、Chrome和Firfox的HTML固定表头例子

 
阅读更多

该代码来自于 http://www.ntsfjc.com/blog/post/15.html ,代码就不重复贴了,就贴个图吧。

 

 演示效果

 

==================================================================

下面是另一个表头固定的例子,经测试在IE7、Chrome和Firefox上都没问题:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Creative Pharmaceutical</title>
<link href="css/common.css" rel="stylesheet" type="text/css"/>
<style type="text/css">
BODY {
	font-size: 0.75em;
	background-color: #f2f5f0 !important;
	color: #000000;
	margin-top: 0px;
	margin-bottom: 0px;
}

TABLE {
	/*border: 1px #19197a solid;*/
	border: 0px #19197a solid;
	border-collapse: collapse;
	/* margin-top: -1px; */
}
TABLE TD {
	background-color: #ffffff;
	border: 1px solid;
	color: #000000;
	height: 1.8em;
	text-align: left;
	word-break:break-all;
}
TABLE TH {
	background-color: #5279e7;
	border: 1px solid;
	color: #ffffff;
	height: 1.8em;
	padding-left: 1em;
	text-align: left;
	word-break:break-all;
}
TABLE THEAD TH {
	padding-left: 1px;
	text-align: center;
	white-space: wrap;
}
</style>
<script language="javascript">

var ScrollableTable = {
	init: false,
	_scrollBarWidth : 18,
	set: function(id, width, height, overflowX, center) {
		if (this.init) {
			return;
		}
		if (overflowX == null) {
			overflowX = false;
		}
		if (center == null) {
			center = false;
		}
		var masterTable = document.getElementById(id); 

		if (masterTable == null) {
			alert("Err \n no table ");
			return;
		}
		if (masterTable.tHead == null) {
			alert("err\n no <THEAD>");
			return;
		}

		if (masterTable.caption != null) {
			masterTable.caption.innerHTML = ""; 
		}

		var thHeight = masterTable.tHead.offsetHeight;
		var tableHeader = masterTable.cloneNode(true);

		tableHeader.id = tableHeader.id + "_H";
		while (tableHeader.tBodies[0].rows.length) {
			tableHeader.tBodies[0].deleteRow(0);
		}
		tableHeader.style.position = "absolute";
		tableHeader.style.left = "0";
		tableHeader.style.top = "0";

		var divHeader = document.createElement("div");
		divHeader.id = "D_" + tableHeader.id;
		divHeader.style.width = width + "px";
		divHeader.style.height = thHeight + "px";
		divHeader.style.overflow = "hidden";
		divHeader.style.position = "relative";
		divHeader.appendChild(tableHeader);

		masterTable.parentNode.insertBefore(divHeader, masterTable);

		var tableBody = masterTable.cloneNode(true);
		tableBody.id = tableBody.id + "_B";
		tableBody.deleteTHead();

		var divBody = document.createElement("div");
		divBody.id = "D_" + tableBody.id;
		divBody.style.width = (width + this._scrollBarWidth) + "px";
		divBody.style.height = height + "px";
		if (overflowX) {
			divBody.style.overflow  = "scroll";
		} else {
			divBody.style.overflowY = "scroll";
			divBody.style.overflowX = "auto";
		}
		if (center) {
			divBody.style.marginLeft  = this._scrollBarWidth + "px";
		}
		divBody.appendChild(tableBody);

		masterTable.parentNode.insertBefore(divBody, masterTable);
		masterTable.parentNode.removeChild(masterTable);

		divBody.onscroll = function() {
			ScrollableTable._onscroll(divHeader.id, divBody.id)
		};

		this.init = true;
	},
	
	_onscroll: function(divHeaderId, divBodyId) {
		var divHeader = document.getElementById(divHeaderId);
		var divBody = document.getElementById(divBodyId);
		divHeader.firstChild.style.left =  "-" + divBody.scrollLeft + "px";
	}
}
</script>

</head>
<body>
<TABLE id="DataTable" style="width:600px">
  <THEAD>
  <tr>
    <th style="width:100px">Colum01</th>
    <th style="width:100px">Colum02</th>
    <th style="width:100px">Colum03</th>
    <th style="width:100px">Colum04</th>
    <th style="width:100px">Colum05</th>
    <th style="width:100px">Colum06</th>
    </tr>
  </THEAD> 	
  <TBODY>
  <tr>
    <td style="width:100px">QQQQQQQQQQQQQQQQQQQQ</td>
    <td style="width:100px">AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</td>
    <td style="width:100px">BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB</td>
    <td style="width:100px">CCCCCCCCCCCCCCCCCCCCCCCCCCCCC</td>
    <td style="width:100px">DDDDDDDDDDDDDDDDDDDDDDDD</td>
    <td style="width:100px">EEEEEEEEEEEEEEEEEEEEE</td>
    </tr>
  <tr>
    <td style="width:100px">QQQQQQQQQQQQQQQQQQQQ</td>
    <td style="width:100px">AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</td>
    <td style="width:100px">BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB</td>
    <td style="width:100px">CCCCCCCCCCCCCCCCCCCCCCCCCCCCC</td>
    <td style="width:100px">DDDDDDDDDDDDDDDDDDDDDDDD</td>
    <td style="width:100px">EEEEEEEEEEEEEEEEEEEEE</td>
  </tr>
  <tr>
    <td style="width:100px">QQQQQQQQQQQQQQQQQQQQ</td>
    <td style="width:100px">AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</td>
    <td style="width:100px">BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB</td>
    <td style="width:100px">CCCCCCCCCCCCCCCCCCCCCCCCCCCCC</td>
    <td style="width:100px">DDDDDDDDDDDDDDDDDDDDDDDD</td>
    <td style="width:100px">EEEEEEEEEEEEEEEEEEEEE</td>
  </tr>
  <tr>
    <td style="width:100px">QQQQQQQQQQQQQQQQQQQQ</td>
    <td style="width:100px">AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</td>
    <td style="width:100px">BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB</td>
    <td style="width:100px">CCCCCCCCCCCCCCCCCCCCCCCCCCCCC</td>
    <td style="width:100px">DDDDDDDDDDDDDDDDDDDDDDDD</td>
    <td style="width:100px">EEEEEEEEEEEEEEEEEEEEE</td>
  </tr>
  <tr>
    <td style="width:100px">QQQQQQQQQQQQQQQQQQQQ</td>
    <td style="width:100px">AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</td>
    <td style="width:100px">BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB</td>
    <td style="width:100px">CCCCCCCCCCCCCCCCCCCCCCCCCCCCC</td>
    <td style="width:100px">DDDDDDDDDDDDDDDDDDDDDDDD</td>
    <td style="width:100px">EEEEEEEEEEEEEEEEEEEEE</td>
  </tr>
  <tr>
    <td style="width:100px">QQQQQQQQQQQQQQQQQQQQ</td>
    <td style="width:100px">AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</td>
    <td style="width:100px">BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB</td>
    <td style="width:100px">CCCCCCCCCCCCCCCCCCCCCCCCCCCCC</td>
    <td style="width:100px">DDDDDDDDDDDDDDDDDDDDDDDD</td>
    <td style="width:100px">EEEEEEEEEEEEEEEEEEEEE</td>
  </tr>
  <tr>
    <td style="width:100px">QQQQQQQQQQQQQQQQQQQQ</td>
    <td style="width:100px">AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</td>
    <td style="width:100px">BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB</td>
    <td style="width:100px">CCCCCCCCCCCCCCCCCCCCCCCCCCCCC</td>
    <td style="width:100px">DDDDDDDDDDDDDDDDDDDDDDDD</td>
    <td style="width:100px">EEEEEEEEEEEEEEEEEEEEE</td>
  </tr>
  <tr>
    <td style="width:100px">QQQQQQQQQQQQQQQQQQQQ</td>
    <td style="width:100px">AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</td>
    <td style="width:100px">BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB</td>
    <td style="width:100px">CCCCCCCCCCCCCCCCCCCCCCCCCCCCC</td>
    <td style="width:100px">DDDDDDDDDDDDDDDDDDDDDDDD</td>
    <td style="width:100px">EEEEEEEEEEEEEEEEEEEEE</td>
  </tr>
  <tr>
    <td style="width:100px">QQQQQQQQQQQQQQQQQQQQ</td>
    <td style="width:100px">AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</td>
    <td style="width:100px">BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB</td>
    <td style="width:100px">CCCCCCCCCCCCCCCCCCCCCCCCCCCCC</td>
    <td style="width:100px">DDDDDDDDDDDDDDDDDDDDDDDD</td>
    <td style="width:100px">EEEEEEEEEEEEEEEEEEEEE</td>
  </tr>
  <tr>
    <td style="width:100px">QQQQQQQQQQQQQQQQQQQQ</td>
    <td style="width:100px">AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</td>
    <td style="width:100px">BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB</td>
    <td style="width:100px">CCCCCCCCCCCCCCCCCCCCCCCCCCCCC</td>
    <td style="width:100px">DDDDDDDDDDDDDDDDDDDDDDDD</td>
    <td style="width:100px">EEEEEEEEEEEEEEEEEEEEE</td>
  </tr>
  <tr>
    <td style="width:100px">QQQQQQQQQQQQQQQQQQQQ</td>
    <td style="width:100px">AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</td>
    <td style="width:100px">BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB</td>
    <td style="width:100px">CCCCCCCCCCCCCCCCCCCCCCCCCCCCC</td>
    <td style="width:100px">DDDDDDDDDDDDDDDDDDDDDDDD</td>
    <td style="width:100px">EEEEEEEEEEEEEEEEEEEEE</td>
  </tr>
  <tr>
    <td style="width:100px">QQQQQQQQQQQQQQQQQQQQ</td>
    <td style="width:100px">AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</td>
    <td style="width:100px">BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB</td>
    <td style="width:100px">CCCCCCCCCCCCCCCCCCCCCCCCCCCCC</td>
    <td style="width:100px">DDDDDDDDDDDDDDDDDDDDDDDD</td>
    <td style="width:100px">EEEEEEEEEEEEEEEEEEEEE</td>
  </tr>
  <tr>
    <td style="width:100px">QQQQQQQQQQQQQQQQQQQQ</td>
    <td style="width:100px">AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</td>
    <td style="width:100px">BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB</td>
    <td style="width:100px">CCCCCCCCCCCCCCCCCCCCCCCCCCCCC</td>
    <td style="width:100px">DDDDDDDDDDDDDDDDDDDDDDDD</td>
    <td style="width:100px">EEEEEEEEEEEEEEEEEEEEE</td>
  </tr>
  <tr>
    <td style="width:100px">QQQQQQQQQQQQQQQQQQQQ</td>
    <td style="width:100px">AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</td>
    <td style="width:100px">BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB</td>
    <td style="width:100px">CCCCCCCCCCCCCCCCCCCCCCCCCCCCC</td>
    <td style="width:100px">DDDDDDDDDDDDDDDDDDDDDDDD</td>
    <td style="width:100px">EEEEEEEEEEEEEEEEEEEEE</td>
  </tr>
  <tr>
    <td style="width:100px">QQQQQQQQQQQQQQQQQQQQ</td>
    <td style="width:100px">AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</td>
    <td style="width:100px">BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB</td>
    <td style="width:100px">CCCCCCCCCCCCCCCCCCCCCCCCCCCCC</td>
    <td style="width:100px">DDDDDDDDDDDDDDDDDDDDDDDD</td>
    <td style="width:100px">EEEEEEEEEEEEEEEEEEEEE</td>
  </tr>
  <tr>
    <td style="width:100px">QQQQQQQQQQQQQQQQQQQQ</td>
    <td style="width:100px">AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</td>
    <td style="width:100px">BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB</td>
    <td style="width:100px">CCCCCCCCCCCCCCCCCCCCCCCCCCCCC</td>
    <td style="width:100px">DDDDDDDDDDDDDDDDDDDDDDDD</td>
    <td style="width:100px">EEEEEEEEEEEEEEEEEEEEE</td>
  </tr>
  <tr>
    <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
    </tr>
  <tr>
    <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
    </tr>
  <tr>
    <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
    </tr>
</TBODY>
</TABLE>
<SCRIPT type="text/javascript">
if(document.getElementById("DataTable")){
	ScrollableTable.set('DataTable', 600, 500,true);
}
</script>
</body>
</html>
 
  • 大小: 74.9 KB
分享到:
评论
1 楼 holy_night 2012-08-21  
[flash=200,200][/flash][u][/u]
引用
[img][/img][url][/url]
[flash=200,200][/flash][size=x-small][/size][align=center][/align][color=red][/color]

相关推荐

    兼容ie,firfox,chrome的页面广告随机飘动效果

    这个项目名为"兼容ie, firfox, chrome的页面广告随机飘动效果",旨在实现一个能在主流浏览器(Internet Explorer, Firefox, Chrome)上运行的广告飘动特效,确保跨浏览器的兼容性。这种效果通常通过JavaScript库,...

    一款轻量、无依赖的 相同 name 的表单验证,动态验证,兼容 chrome 、firfox 、IE9 +的JavaScript

    本文将深入探讨一个针对Web应用开发的轻量级JavaScript验证组件,该组件专为处理具有相同name属性的表单字段进行动态验证,并且兼容主流浏览器,包括Chrome、Firefox以及IE9及以上版本。 表单验证是Web应用中不可或...

    树形菜单javascript(兼容IE,Firfox,刷新记忆)

    在本案例中,我们关注的是一个用JavaScript实现的树形菜单,它具有优秀的跨平台兼容性和高效的性能。让我们深入探讨这个JavaScript树形菜单的特点和相关知识点。 首先,这个树形菜单设计的核心在于其对不同浏览器的...

    前端设计IE6IE7IE8IE9FF问题汇总IE和FirFox兼容问题[参照].pdf

    在前端开发过程中,确保网页在不同的浏览器间具有良好的兼容性是一项重要的任务。本文主要讨论了在Internet Explorer(IE)6、7、8、9以及Firefox之间存在的若干常见问题及其解决方案。以下是一些关键知识点的详细...

    单击其它区域关闭日历控件(兼容firfox与IE)

    "单击其它区域关闭日历控件(兼容firfox与IE)"这个主题涉及到的是一个常见的前端交互功能,即如何通过JavaScript或者特定的库来实现日历控件的交互优化,使其在用户选择日期后能够通过点击页面上的其他区域来关闭。...

    FireFox Chrome IE浏览器调试JavaScript

    ### FireFox Chrome IE浏览器调试JavaScript #### 一、Firebug简介 Firebug是一款专为Firefox浏览器设计的强大开发插件,自推出以来便受到广大Web开发者的喜爱与推崇。随着Web技术的发展,对Web开发人员的要求...

    因纳伟盛firfox、chrome插件安装包1.1.rar

    标题中的“因纳伟盛firfox、chrome插件安装包1.1.rar”指的是一个针对Firefox和Chrome浏览器的插件安装文件,版本为1.1,以RAR格式压缩。这个安装包是由因纳伟盛公司提供的,可能用于增强或扩展浏览器功能,特别是与...

    HttpWatch(支持firfox、IE)

    HttpWatch是强大的网页数据分析工具.集成在Internet Explorer工具栏.包括网页摘要.Cookies管理....每一个HTTP记录都可以详细的分析其 Cookies、消息头、字符查询等信息。支持HTTPS及分析报告输出为XML、CSV等格式

    firfox和ie6的js执行效率比较

    标题中的“firfox”应该是“Firefox”,这是一款由Mozilla开发的开源网络浏览器,而“ie6”指的是微软的Internet Explorer 6,这是一个较旧版本的IE浏览器。本话题主要探讨的是JavaScript在Firefox和IE6这两款浏览器...

    firfox firfox firfox firfox

    firfoxfirfoxfirfoxfirfoxfirfoxfirfoxfirfoxfirfoxfirfoxfirfoxfirfoxfirfoxfirfoxfirfoxfirfoxfirfoxfirfoxfirfoxfirfoxfirfoxfirfoxfirfoxfirfox

    判断当前使用的浏览器版本ie、firfox等

    @ */` 是一个条件注释,只在IE中被解释,用于检测旧版IE(5-8)。 `document.documentMode` 属性则在IE9及以上版本中存在,表示当前页面模拟的IE模式。 2. **Firefox浏览器的判断**: Firefox的`userAgent`字符串...

    javascript表格可调整列宽(兼容firfox/IE)

    本demo使用jQuery包,实现表格列宽可拖拽功能,并实现页面reset时的重新布局。使用jQuery,方便函数的调用,给要处理的表格添加id 后,直接调用$("#id").movedTh()即可方便实现,修改了firfox的兼容性。

    selenium webdriver+chrome插件.zip

    Selenium IDE是一个记录和回放工具,用于创建和调试自动化测试脚本。在Chrome上,描述中提到的"selenium-ide3.17-解压后,chrome进行安装.zip"很可能是Selenium IDE的Chrome版本,用户可以通过它快速录制测试场景并...

    firfox最最好用的插件

    标题中的“firfox最最好用的插件”指的是Firefox浏览器的一些最受欢迎和实用的扩展插件。这些插件能够提升Firefox的使用体验,增加各种功能,满足用户的不同需求。以下是各个插件的详细介绍: 1. **Coral IE Tab**:...

    Flex调试工具 IE Firfox

    - **Adobe Scout**(已弃用):曾是Adobe提供的一个性能分析工具,可以深入分析Flex应用的内存占用、渲染和事件处理等性能问题。不过,目前已被Adobe Animate取代,推荐使用更现代的性能分析工具如Adobe Experience...

    firfox and firedebug

    而Firebug是Firefox的一个插件,它允许开发者对网页的HTML、CSS以及JavaScript进行实时编辑、调试和分析。 Firebug的详细功能包括: 1. **HTML查看和编辑**:Firebug可以让你实时查看和编辑网页的HTML结构,快速...

    火狐浏览器兼容IE加载64位COM组件的方法以及所需资源

    然而,由于安全性考虑,火狐已经逐步淘汰了NPAPI,所以你需要找到一个替代方案,如"ActiveX Control Container for Firefox",这是一个可以承载并执行ActiveX控件的插件。 3. **安装和配置插件**:下载并安装所选...

    C# Firfox geckofx-33.0

    【C# Firfox geckofx-33.0】是一个基于C#的开源项目,它使得.NET开发者能够在他们的应用程序中嵌入Firefox浏览器引擎。这个项目的核心是Geckofx,一个封装了Mozilla的Gecko渲染引擎的库,用于提供Web浏览功能。Gecko...

Global site tag (gtag.js) - Google Analytics