我如何指定一个td标签应该跨越所有列(当表中的列的确切数量将是可变的/难以确定当HTML被呈现)?w3schools提到你可以使用colspan="0",但它没有确切地说什么浏览器支持这个值(IE 6在我们的列表中支持)。
看起来,将colspan设置为大于您可能拥有的理论列数量的值是可行的,但如果您将表布局设置为fixed,则它将不起作用。对colspan使用一个大数字的自动布局有什么缺点吗?有更正确的方法吗?
我如何指定一个td标签应该跨越所有列(当表中的列的确切数量将是可变的/难以确定当HTML被呈现)?w3schools提到你可以使用colspan="0",但它没有确切地说什么浏览器支持这个值(IE 6在我们的列表中支持)。
看起来,将colspan设置为大于您可能拥有的理论列数量的值是可行的,但如果您将表布局设置为fixed,则它将不起作用。对colspan使用一个大数字的自动布局有什么缺点吗?有更正确的方法吗?
当前回答
我有IE 7.0, Firefox 3.0和Chrome 1.0
一个TD中的colspan="0"属性不会跨越任何上述浏览器中的所有TD。
可能不推荐作为适当的标记实践,但如果您给出的colspan值高于可能的总no。那么TD就张成了所有的列。
当表格布局CSS属性被设置为fixed时,这将不起作用。
再说一次,这不是完美的解决方案,但在上面提到的3个浏览器版本中,当表布局CSS属性是自动的时候,这似乎是有效的。
其他回答
这里有人觉得为了这个看似微不足道的问题而深入到JS中似乎有点过分了吗?
纯CSS
繁荣!我有一个纯CSS解决方案为您提供!下面是一个例子,你只需要向你想要张成所有列的行添加一个类。然后CSS将使第一个<td>元素跨越全宽度,并隐藏其余的<td>元素。(你必须使用可见性:hidden;and NOT display:无;对于这个)。
注意:这个方法至少需要两个单元格才能很好地呈现,如果你保持<td>元素的正确数量,CSS将呈现得最好——不要删除任何元素来为span元素腾出空间。这将有助于确保单元格/行仍然正常运行。
例子
/* standard styling css */ table { border-collapse: collapse; } table, tr, td { border: 1px solid black; } td { padding: 3px; } /* make full width class span the whole table */ .full-span { position:relative; } .full-span > * { visibility: hidden; border:0; } .full-span > *:nth-child(1) { display: block; visibility: unset; position:absolute; } <table> <tbody> <tr> <td>A1</td> <td>A2</td> <td>A3</td> <td>A4</td> </tr> <tr class="full-span"> <td>B1 long text</td> <td>B2</td> <td>B3</td> <td>B4</td> </tr> <tr> <td>C1</td> <td>C2</td> <td>C3</td> <td>C4</td> </tr> <tr> <td>D1</td> <td>D2</td> <td>D3</td> <td>D4</td> </tr> </tbody> </table>
小贴士!
如果你用PHP/JS动态生成你的表,这可能会清理你的一些代码。假设您正在遍历一个2D数组以创建一个表:对于需要跨出所有列的每一行,您需要添加一些逻辑来计算列的数量,添加colspan属性,添加构成表的全宽度所需的任何剩余<td>元素,等等。
使用我的方法,您可以遍历所有列并输出它们,只需将类包含在父行中。
对于IE 6,你会希望colspan等于表中的列数。如果你有5列,那么你需要:colspan="5"。
原因是IE处理colspan的方式不同,它使用HTML 3.2规范:
IE实现了HTML 3.2的定义,它将colspan=0设置为colspan=1。
这个bug有很好的文档记录。
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>
下面是一个简洁的es6解决方案(类似于Rainbabba的答案,但没有jQuery)。
Array.from(document.querySelectorAll('[data-colspan-max]')).forEach(td => { let table = td; while (table && table.nodeName !== 'TABLE') table = table.parentNode; td.colSpan = Array.from(table.querySelector('tr').children).reduce((acc, child) => acc + child.colSpan, 0); }); html { font-family: Verdana; } tr > * { padding: 1rem; box-shadow: 0 0 8px gray inset; } <table> <thead> <tr> <th>Header 1</th> <th>Header 2</th> <th>Header 3</th> <th>Header 4</th> <th>Header 5</th> <th>Header 6</th> </tr> </thead> <tbod><tr> <td data-colspan-max>td will be set to full width</td> </tr></tbod> </table>
如果您正在使用jQuery(或者不介意添加它),这将比任何这些技巧更好地完成工作。
function getMaxColCount($table) {
var maxCol = 0;
$table.find('tr').each(function(i,o) {
var colCount = 0;
$(o).find('td:not(.maxcols),th:not(.maxcols)').each(function(i,oo) {
var cc = Number($(oo).attr('colspan'));
if (cc) {
colCount += cc;
} else {
colCount += 1;
}
});
if(colCount > maxCol) {
maxCol = colCount;
}
});
return maxCol;
}
为了简化实现,我装饰任何td/th我需要调整类,如“maxCol”,然后我可以做以下工作:
$('td.maxcols, th.maxcols').each(function(i,o) {
$t = $($(o).parents('table')[0]); $(o).attr('colspan', getMaxColCount($t));
});
如果你发现一个实现不适用,不要抨击答案,在评论中解释,如果它可以覆盖,我会更新。