gpt4 book ai didi

忽略元素样式宽度的 HTML 表格

转载 作者:太空狗 更新时间:2023-10-29 15:25:11 24 4
gpt4 key购买 nike

忽略元素样式宽度的 HTML 表格

我有一个 HTML 表格,其中某些单元格的文本内容很长。

我想使用 jQuery 为这些单元格指定宽度(以像素为单位),但呈现的表格只是忽略了给定的宽度。

有没有办法强制表格遵守这个宽度?

谢谢!


JSFiddle:http://jsfiddle.net/sangil/6hejy/35/

(如果您检查单元格,您可以看到计算出的宽度与元素样式的宽度不同)


HTML:

<div id="tblcont" class="tblcont">
<table id="pivot_table">
<tbody>
<tr>
<th id="h0" >product</th>
<th id="h1" >price</th>
<th id="h2" >change</th>
</tr>
<tr>
<!-- this is the cell causing trouble -->
<td id="c00" >Acer 2400 aaaaaaaaaaaaaaaaaaaaaaaaaa</td>
<td id="c01" >3212</td>
<td id="c02" >219</td>

</tr>
<tr>
<td id="c10" >Acer</td>
<td id="c11" >3821</td>
<td id="c12" >206</td>

</tr>
</tbody>
</table>
</div>

CSS:

.tblcont {
overflow: hidden;
width: 500px;
}

table {
table-layout: fixed;
border-collapse: collapse;
overflow-x: scroll;
border-spacing:0;
width: 100%;
}

th, td {
overflow: hidden;
text-overflow: ellipsis;
word-wrap: break-word;
}

th {
height: 50px;
}

Javascript:

$(document).ready(function() {

// THIS LINE HAS NO EFFECT!
$('#c00').width(30);
});​

最佳答案

我可以从你的 fiddle 中看出你已经很好地掌握了如何就地截断这个词。我认为您可能会发现执行以下操作很有用:

<table>
<colgroup>
<col style="width: 30px;" /> <!-- this style affects the whole 1st column -->
<col />
<col />
</colgroup>

<!-- the rest of your table here -->
</table>

此方法以符合 HTML 规范的方式工作 - 并将调整整个列的大小。如果您改为将单元格的 display 更改为 inline-block,如上所述,将会发生的情况是您将单元格从表格的流程中取出 - 以及其他风格更改可能会停止工作。

通过使用上面的代码设置整个 col 的样式,您可以使用 table 元素的 table-layout: fixed 样式来代替您的优势。

此外 - 我注意到您已将单元格设置为使用 text-overflow: ellipsis; 查看 this article在 quirksmode 上了解为什么它不起作用。您需要的修复是进行以下编辑:

th, td {
border: solid #4682B4 1px;
overflow: hidden;
text-overflow: ellipsis;
word-wrap: break-word;
text-align: center;

white-space: nowrap; /* Add this line */
}

关于忽略元素样式宽度的 HTML 表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13073379/

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