无法回避的横向滚动条

J2002 2008-08-04 05:32:59
项目要求一个div(overflow-x: scroll;overflow-y: scroll;)内含有一个table(width:100%)
也就是希望
当表格实际宽度大于div宽度时需要出现横向滚动条,
当表格实际宽度小于div宽度时,表格横向充满div,但不需要横向滚动条。
并且div始终保持一个垂直滚动条。

问题是,表格实际宽度小于div宽度时,依然出现横向滚动条,原因是ie显示时计算出来的表格宽度=div的宽度,准确的宽度应该=div的宽度-垂直滚动条的宽度。

大家看看有没有办法解决。

代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<style type="text/css">
.yesScroll {
width: 100%;
background-color: #666666;
overflow-x: scroll;
overflow-y: scroll;
padding: 0px;
}
.tblDetail {
background-color: white;
border-width: 1px;
border-color: snow;
border-style: solid;
border-collapse: collapse;
margin: 1px;
float: left;
}
.thCenter {
font-size: 12px;
font-weight: normal;
color: #113388;
background-color: #d4e3ff;
text-align: center;
vertical-align: middle;
border-collapse: collapse;
border-right-width: 1px;
border-bottom-width: 3px;
border-right-color: #a1a1a1;
border-bottom-color: #a1a1a1;
border-right-style: solid;
border-bottom-style: solid;
height: 20px;
padding-left: 4px;
padding-right: 4px;
white-space:nowrap;
}
.trOdd {
background-color:#ffffff;
}
.trEven {
background-color:#f0ffff;
}
.tdLeft {
font-size: 12px;
text-align: left;
vertical-align: middle;
margin-left: 2px;
margin-right: 2px;
border-collapse: collapse;
border-right-width: 1px;
border-bottom-width: 1px;
border-right-color: #a1a1a1;
border-bottom-color: #a1a1a1;
border-right-style: solid;
border-bottom-style: solid;
height: 20px;
padding-left: 4px;
padding-right: 4px;
white-space:nowrap;
color: #000000;
}
</style>
</head>
<body>
<table style="width:770px" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<div id="master" class="yesScroll" style="width:770px;height:225px">
<table id="myTable" cellspacing="0" cellpadding="0" class="tblDetail" style="width:100%">
<thead>
<tr>
<th nowrap class="thCenter">AAA</th>
<th nowrap class="thCenter">BBB</th>
</tr>
</thead>
<tbody>
<tr onclick="lineSelect(this);" class="trOdd">
<td nowrap class="tdLeft">PDF</td>
<td nowrap class="tdLeft">111</td>
</tr>
<tr onclick="lineSelect(this);" class="trEven">
<td nowrap class="tdLeft">CSV</td>
<td nowrap class="tdLeft">222</td>
</tr>
<tr onclick="lineSelect(this);" class="trOdd">
<td nowrap class="tdLeft">TXT</td>
<td nowrap class="tdLeft">333</td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
</table>
</body>
</html>
...全文
236 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
shaovey 2008-08-05
  • 打赏
  • 举报
回复
table在div里面,还使用100%继承其宽度,又何来“表格实际宽度小于div”呢?
cftea 2008-08-05
  • 打赏
  • 举报
回复
我想了个办法,就是利用 JavaScript 来实现兼容。
看代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html>
<head>
<style type="text/css">
.yesScroll {
width: 100%;
background-color: #666666;
overflow-x: auto;
overflow-y: scroll;
padding: 0px;
}
.tblDetail {
background-color: white;
border-width: 1px;
border-color: snow;
border-style: solid;
border-collapse: collapse;
margin: 0px;
float: left;
}
.thCenter {
font-size: 12px;
font-weight: normal;
color: #113388;
background-color: #d4e3ff;
text-align: center;
vertical-align: middle;
border-collapse: collapse;
border-right-width: 1px;
border-bottom-width: 3px;
border-right-color: #a1a1a1;
border-bottom-color: #a1a1a1;
border-right-style: solid;
border-bottom-style: solid;
height: 20px;
padding-left: 4px;
padding-right: 4px;
white-space:nowrap;
}
.trOdd {
background-color:#ffffff;
}
.trEven {
background-color:#f0ffff;
}
.tdLeft {
font-size: 12px;
text-align: left;
vertical-align: middle;
margin-left: 2px;
margin-right: 2px;
border-collapse: collapse;
border-right-width: 1px;
border-bottom-width: 1px;
border-right-color: #a1a1a1;
border-bottom-color: #a1a1a1;
border-right-style: solid;
border-bottom-style: solid;
height: 20px;
padding-left: 4px;
padding-right: 4px;
white-space:nowrap;
color: #000000;
}
</style>
</head>
<body>
<table style="width:770px" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<div id="master" class="yesScroll" style="width:770px;height:225px">
<table id="myTable" cellspacing="0" cellpadding="0" class="tblDetail" style="width:100%;">
<thead>
<tr>
<th nowrap class="thCenter">AAA </th>
<th nowrap class="thCenter">BBB </th>
</tr>
</thead>
<tbody>
<tr onclick="lineSelect(this);" class="trOdd">
<td nowrap class="tdLeft">PDF </td>
<td nowrap class="tdLeft">111 </td>
</tr>
<tr onclick="lineSelect(this);" class="trEven">
<td nowrap class="tdLeft">CSV </td>
<td nowrap class="tdLeft">222 </td>
</tr>
<tr onclick="lineSelect(this);" class="trOdd">
<td nowrap class="tdLeft">TXT </td>
<td nowrap class="tdLeft">333 </td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
</table>
<script type="text/javascript">
var myTable = document.getElementById("myTable");
if (myTable.offsetParent.clientWidth < myTable.offsetParent.offsetWidth)
{
//IE 的 clientWidth 不包括边框,FF 包括。
myTable.style.width = myTable.offsetParent.clientWidth + "px";
}
</script>
</body>
</html>

改了几个地方,包括上面提到的,以及一些兼容性问题,比如我把 margion 设为了 0。
cftea 2008-08-05
  • 打赏
  • 举报
回复
overflow-x: scroll;
这里应该改为
overflow-x: auto;
 
不过宽度那个问题还真有点复杂,一起研究。
内容概要:本文围绕“分布式电源接入配电网承载力评估方法”的研究展开,重点复现了一项基于双层鲸鱼优化算法求解的核心学术论文,结合Matlab编程实现,对IEEE 33节点配电网系统进行建模与仿真分析。研究旨在科学评估在大规模分布式电源接入背景下配电网的承载能力,构建了综合考虑系统运行安全性、电能质量、网络损耗及电压稳定性等多重约束条件的优化评估模型,并采用高效的智能优化算法进行求解,有效提升了评估精度与计算效率,为新能源并网规划、电网扩容改造及运行决策提供了可靠的理论依据和技术支撑。该资源不仅提供完整的代码实现,还深入解析算法设计逻辑与模型构建流程,具有较强的科研复现价值和工程参考意义。; 适合人群:具备电力系统分析基础理论知识和Matlab编程能力,从事新能源并网、智能配电网规划、电力系统优化、分布式能源管理等方向的研究生、科研人员及电力行业工程技术人员。; 使用场景及目标:① 学习并掌握分布式电源接入对配电网影响的量化评估方法;② 深入理解双层优化架构与智能算法(如鲸鱼优化算法)在复杂电力系统问题中的应用机制;③ 获取可运行、可调试的Matlab代码资源,用于科研论文复现、课题研究仿真、课程设计或工程项目前期论证。; 阅读建议:此资源以核心论文的技术路线为基础,强调理论与实践相结合。建议读者在阅读过程中结合电力系统潮流计算、约束优化等基础知识,逐步理解模型构建思路,并动手运行与调试所提供的Matlab代码,通过参数调整与结果分析深化对算法性能与工程适用性的认知,从而真正实现从“看懂”到“掌握”的转化。

61,121

社区成员

发帖
与我相关
我的任务
社区描述
层叠样式表(英文全称:Cascading Style Sheets)是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机语言。
社区管理员
  • HTML(CSS)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧