<!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>
<title></title>
</head>
<body>
<div>
<table id="tabListHeader" border="1">
<tr>
<td>
ID
</td>
<td>
名称
</td>
</tr>
</table>
<div id="list" style="height: 60px; overflow: auto">
<table id="tabList" border="1" width="100%">
<tr>
<td>
1
</td>
<td>
Jerry
</td>
</tr>
<tr>
<td>
2
</td>
<td>
Tom.Zhang
</td>
</tr>
<tr>
<td>
3
</td>
<td>
Ryo
</td>
</tr>
<tr>
<td>
4
</td>
<td>
King
</td>
</tr>
</table>
</div>
</div>
</body>
</html>
<script type="text/javascript">
var tabList = document.getElementById("tabList");
var tabListHeader = document.getElementById("tabListHeader");
if (typeof (tabListHeader) == "undefined") {
throw new Error("表头未定义");
}
if (typeof (tabListHeader.rows[0]) == "undefined") {
throw new Error("表头行未定义");
}
if (typeof (tabList) == "undefined") {
throw new Error("列表未定义");
}
// tabListHeader.style = tabList.style;
if (tabList.rows.length > 0) {
//alert(tabList.rows[0].cells.length);
for (var i = 0; i <= tabList.rows[0].cells.length - 1; i++) {
//alert(tabList.rows[0].cells[i].clientWidth);
if (typeof (tabListHeader.rows[0].cells[i]) == "undefined") {
throw new Error("列头未定义");
}
else {
var tdWidth = tabList.rows[0].cells[i].clientWidth;
var tdHeaderWidth = tabListHeader.rows[0].cells[i].clientWidth
alert(tdWidth);
alert(tdHeaderWidth);
if (tdWidth > tdHeaderWidth) {
//tabListHeader.rows[0].cells[i].clientWidth = tdWidth;
tabListHeader.rows[0].cells[i].style.width = tdWidth + "px";
}
else {
//tabList.rows[0].cells[i].clientWidth = tdHeaderWidth;
tabList.rows[0].cells[i].style.width = tdHeaderWidth + "px";
}
}
}
}
</script>以上是我自己用js写表头的代码,但是有几个地方不太理想,大家帮看看
还有怎么写更好,
如上面
tabListHeader.style = tabList.style;
这一句我的想法是将列表的样式全都赋值给表头,
但提示Style为只读,那这里要怎么写呢?
tabList.rows[0].cells[i].style.width = tdHeaderWidth + "px";
这种写法的话他的宽有问题
而我用
tabList.rows[0].cells[i].clientWidth = tdHeaderWidth;
也是提示说clientWidth为只读
那应该怎么写呢?谢谢
解决方案 »
- javascript replace的问题
- js问题,请高手帮忙
- 正则表达式,求解
- 如何给新增dom元素 套用jquery ui的autocomplete控件
- JS高手,Option的底色问题!!在线等!!
- 来者有分:看看这个网站 www.inhi.cn,花了我三天的时间做的,大家提点意见呀!
- 怎么分析出ajax请求的服务地址
- 模态对话框window.showModalDialog怎么触发父窗口的javascript事件?
- 简单问题,关于下拉菜单,急用
- 请问关于控制打印样式的问题
- Firefox 是不是对posLeft或posTop不兼容?有图
- 请高手解析一下javascript同步与异步区别
<style type="text/css"> .list
{
height: 60px;
overflow: auto
}
</style> <div id="list" class="list">tabListHeader.className = tabList.className;
<!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>
<title></title>
<style type="text/css">
html, body
{
margin: 0px;
padding: 0px;
}
</style>
</head>
<body>
<div>
<table id="tabListHeader" border="1" cellspacing="0">
<tr>
<td>
ID
</td>
<td>
名称
</td>
</tr>
</table>
<div id="list" style="height: 60px; width: 100%; overflow: auto">
<table id="tabList" border="1" cellspacing="0" width="100%">
<tr>
<td>
1
</td>
<td>
Jerry
</td>
</tr>
<tr>
<td>
2
</td>
<td>
Tom.Zhang
</td>
</tr>
<tr>
<td>
3
</td>
<td>
Ryo
</td>
</tr>
<tr>
<td>
4
</td>
<td>
King
</td>
</tr>
</table>
</div>
</div>
</body>
</html>
<script type="text/javascript">
var tabList = document.getElementById("tabList");
var tabListHeader = document.getElementById("tabListHeader");
if (typeof (tabListHeader) == "undefined") {
throw new Error("表头未定义");
}
if (typeof (tabListHeader.rows[0]) == "undefined") {
throw new Error("表头行未定义");
}
if (typeof (tabList) == "undefined") {
throw new Error("列表未定义");
}
// tabListHeader.style = tabList.style;
if (tabList.rows.length > 0) {
//alert(tabList.rows[0].cells.length);
for (var i = 0; i <= tabList.rows[0].cells.length - 1; i++) {
//alert(tabList.rows[0].cells[i].clientWidth);
if (typeof (tabListHeader.rows[0].cells[i]) == "undefined") {
throw new Error("列头未定义");
}
else {
var tdWidth = tabList.rows[0].cells[i].clientWidth;
var tdHeaderWidth = tabListHeader.rows[0].cells[i].clientWidth
alert(tdWidth);
alert(tdHeaderWidth);
if (tdWidth > tdHeaderWidth) {
//tabListHeader.rows[0].cells[i].clientWidth = tdWidth;
tabListHeader.rows[0].cells[i].style.width = tdWidth - 2 + "px";
}
else {
//tabList.rows[0].cells[i].clientWidth = tdHeaderWidth;
tabList.rows[0].cells[i].style.width = tdHeaderWidth - 2 + "px";
}
}
}
}
</script>以上代码在FireFox中,可以实现我要的效果,
但是在IIS中的滚动条不见了,
麻烦大家指定如何改可以兼容FireFox IE6,7,8谢谢