您的位置
主页 > 网站技术 > 语言编程 > » 正文

JavaScript实现两个Table固定表头根据页面大小自行调整

来源: 站长圈 点击:

正如标题所言两个Table固定表头,可根据页面大小自行调整使用JavaScript实现,具体的示例如下,需要的朋友可以参考一下,希望对大家有所帮助!

复制代码 代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8" 

pageEncoding="UTF-8"%> 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "

http://www.w3.org/TR/html4/loose.dtd"> 

<html> 

<head> 

<title></title> 

<script> 

/** 

* @param oTarget 需要加载scroll的dom对象 

* @param fnHandler 处理的回调函数 

*/ 

function eventHandler(oTarget, fnHandler) { 

if (oTarget.addEventListener) { 

oTarget.addEventListener("scroll", fnHandler, false); 

} else if (oTarget.attachEvent) { 

oTarget.attachEvent("onscroll", fnHandler); 

} else { 

oTarget["onscroll"] = fnHandler; 

}; 

function oo(pContentID, pHeaderID) { 

var p = document.getElementById(pContentID); 

var left = p.scrollLeft; 

var pHeader = document.getElementById(pHeaderID); 

pHeader.scrollLeft = left; 

var disWidth = p.children[0].style.width.replace("px", "") 

- p.style.width.replace("px", ""); 

if (p.scrollLeft >= disWidth) { 

pHeader.style.overflowY = 'scroll'; 

} else { 

pHeader.style.overflowY = 'hidden'; 

window.onload = function onStartLock() { 

var op = document.getElementById("pContentID"); 

eventHandler(op, function() { 

oo("pContentID", "pHeaderID"); 

}); 

}; 

</script> 

</head> 

<body> 

<p id="p1All" 

style="position: absolute; left: 0px; right: 0px; top: 0px; bottom: 0px"> 

<p id="pHeaderID" 

style="margin-right: auto; margin-left: auto; overflow: hidden;"> 

<table border="1" cellspacing="0" 

style="BORDER-COLLAPSE: collapse; font-size: 15px" 

borderColor="#c1dad7" cellPadding="0" bgcolor="#f5fffa" 

width="600px"> 

<Tr style="background: navy; color: white; height: 30px"> 

<Th width="100px">Header A</Th> 

<Th width="100px">Header B</Th> 

<Th width="100px">Header C</Th> 

<Th width="100px">Header D</Th> 

<Th width="100px">Header E</Th> 

<Th width="100px">Header F</Th> 

</Tr> 

</table> 

</p> 

<p id="pContentID" 

style="position: absolute; left: 0px; top: 30.5px; bottom: 0px; right: 0px; overflow: scroll"> 

<table border="1" cellspacing="0" 

style="BORDER-COLLAPSE: collapse; font-size: 15px" 

borderColor="#c1dad7" cellPadding="0" bgcolor="#f5fffa" 

width="600px"> 

<Tr> 

<Td width="100px">A</Td> 

<Td width="100px">B</Td> 

<Td width="100px">C</Td> 

<Td width="100px">D</Td> 

<Td width="100px">E</Td> 

<Td width="100px">F</Td> 

</Tr> 

<Tr> 

<Td width="100px">A</Td> 

<Td width="100px">B</Td> 

<Td width="100px">C</Td> 

<Td width="100px">D</Td> 

<Td width="100px">E</Td> 

<Td width="100px">F</Td> 

</Tr> 

<Tr> 

<Td width="100px">A</Td> 

<Td width="100px">B</Td> 

<Td width="100px">C</Td> 

<Td width="100px">D</Td> 

<Td width="100px">E</Td> 

<Td width="100px">F</Td> 

</Tr> 

<Tr> 

<Td width="100px">A</Td> 

<Td width="100px">B</Td> 

<Td width="100px">C</Td> 

<Td width="100px">D</Td> 

<Td width="100px">E</Td> 

<Td width="100px">F</Td> 

</Tr> 

<Tr> 

<Td width="100px">A</Td> 

<Td width="100px">B</Td> 

<Td width="100px">C</Td> 

<Td width="100px">D</Td> 

<Td width="100px">E</Td> 

<Td width="100px">F</Td> 

</Tr> 

</table> 

</p> 

</p> 

</body> 

</html> 




首页  - 关于站长圈  - 广告服务  - 联系我们  - 关于站长圈  - 网站地图  - 版权声明