gpt4 book ai didi

javascript - 表溢出隐藏?

转载 作者:太空宇宙 更新时间:2023-11-03 18:45:16 24 4
gpt4 key购买 nike

我在这里尝试做的是在一个最大宽度为 300 像素 的 div 中用 Javascript 创建一个表格(成功)。

现在,如果我的表格宽度超过 300 像素,我希望 overflow hidden ,但是即使我设置了 <td>,表格每次都会将其宽度缩小到 300 像素。宽度不同:

<style>
#tablespace {
margin:auto;
position:relative;
width:300px;
overflow:hidden;
}
</style>

<button onclick="CreateTable()" >Try it</button>
<div id="tablespace">
<p id="tablespace"> </p>
</div>

<script>
function CreateTable()
{
var tablecontents = "";
tablecontents = "<table border=1>";
//number of rows
for (var i = 0; i < 3; i ++)
{
tablecontents += "<tr>";
//number of columns
for (var a = 0; a < 5; a ++)
{
tablecontents += "<td height= 50, width= 100>" + i + "</td>";
}
tablecontents += "</tr>";
}
tablecontents += "</table>";
document.getElementById("tablespace").innerHTML = tablecontents;
}
</script>

因此,在我使用单元格高度为 50px 和宽度为 100px 的地方,因为我的 div 的最大宽度为 300px,我应该只能看到 2 个半单元格,然后其余单元格被隐藏,但事实并非如此。

我知道这可能是一个基本错误,但我不知道。

最佳答案

尝试改变这个(我不知道这是否真的已经在代码中):

tablecontents += "<td height= 50, width= 100>" + i + "</td>";

tablecontents += '<td height="50px", width="100px">' + i + "</td>";

顺便说一句,我建议跳过所有这些 HTML3 参数,并通过单独的 CSS 类来完成这一切

tablecontents += '<td class='croppedTable'>' + i + "</td>";

<style>
#tablespace {
margin:auto;
position:relative;
width:300px;
overflow:hidden;
}

.croppedTable {
... some styles ...
}
.croppedTable TD {
... some additional styles ...
width: 100px;
height: 50px;

}
</style>

关于javascript - 表溢出隐藏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16531604/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com