我如何指定一个td标签应该跨越所有列(当表中的列的确切数量将是可变的/难以确定当HTML被呈现)?w3schools提到你可以使用colspan="0",但它没有确切地说什么浏览器支持这个值(IE 6在我们的列表中支持)。

看起来,将colspan设置为大于您可能拥有的理论列数量的值是可行的,但如果您将表布局设置为fixed,则它将不起作用。对colspan使用一个大数字的自动布局有什么缺点吗?有更正确的方法吗?


当前回答

CSS解决方案是理想的,但我无法找到一个,所以这里有一个JavaScript解决方案:对于具有给定类的tr元素,通过选择一个完整的行,计算其td元素及其colSpan属性,并使用el设置加宽的行来最大化它。colSpan = newcolspan;像这样…

var headertablerows = document.getElementsByClassName('max-col-span'); [].forEach.call(headertablerows, function (headertablerow) { var colspan = 0; [].forEach.call(headertablerow.nextElementSibling.children, function (child) { colspan += child.colSpan ? parseInt(child.colSpan, 10) : 1; }); headertablerow.children[0].colSpan = colspan; }); html { font-family: Verdana; } tr > * { padding: 1rem; box-shadow: 0 0 8px gray inset; } <table> <tr class="max-col-span"> <td>1 - max width </td> </tr> <tr> <td>2 - no colspan </td> <td colspan="2">3 - colspan is 2 </td> </tr> </table>

如果使用表头,可能需要调整这一点,但这应该是一种使用100%纯JavaScript的概念验证方法。

其他回答

对于IE 6,你会希望colspan等于表中的列数。如果你有5列,那么你需要:colspan="5"。

原因是IE处理colspan的方式不同,它使用HTML 3.2规范:

IE实现了HTML 3.2的定义,它将colspan=0设置为colspan=1。

这个bug有很好的文档记录。

我有IE 7.0, Firefox 3.0和Chrome 1.0

一个TD中的colspan="0"属性不会跨越任何上述浏览器中的所有TD。

可能不推荐作为适当的标记实践,但如果您给出的colspan值高于可能的总no。那么TD就张成了所有的列。

当表格布局CSS属性被设置为fixed时,这将不起作用。

再说一次,这不是完美的解决方案,但在上面提到的3个浏览器版本中,当表布局CSS属性是自动的时候,这似乎是有效的。

colspan="100%"

它的工作也在电子邮件outlook, gmail....

只是想补充我的经验并回答这个问题。 注意:它只在你有一个预定义的表和一个带有th的tr时有效,但是在你的行中动态加载(例如通过AJAX)。

在这种情况下,您可以计算第一个标题行中有th的数量,并使用它来张成整个列。

当您希望在没有找到结果的情况下转发消息时,可能需要此选项。

在jQuery中,table是你的输入表:

var trs = $(table).find("tr");
var numberColumns = 999;
if (trs.length === 1) {
    //Assume having one row means that there is a header
    var headerColumns = $(trs).find("th").length;
    if (headerColumns > 0) {
        numberColumns = headerColumns;
    }
}

If you want to make a 'title' cell that spans all columns, as header for your table, you may want to use the caption tag (http://www.w3schools.com/tags/tag_caption.asp / https://developer.mozilla.org/en-US/docs/Web/HTML/Element/caption) This element is meant for this purpose. It behaves like a div, but doesn't span the entire width of the parent of the table (like a div would do in the same position (don't try this at home!)), instead, it spans the width of the table. There are some cross-browser issues with borders and such (was acceptable for me). Anyways, you can make it look as a cell that spans all columns. Within, you can make rows by adding div-elements. I'm not sure if you can insert it in between tr-elements, but that would be a hack I guess (so not recommended). Another option would be messing around with floating divs, but that is yuck!

Do

<table>
    <caption style="gimme some style!"><!-- Title of table --></caption>
    <thead><!-- ... --></thead>
    <tbody><!-- ... --></tbody>
</table>

<div>
    <div style="float: left;/* extra styling /*"><!-- Title of table --></div>
    <table>
        <thead><!-- ... --></thead>
        <tbody><!-- ... --></tbody>
    </table>
    <div style="clear: both"></div>
</div>