我想基于每个相应列中第一个<td>
的对齐来设置表头的对齐。基于第一行对齐设置表头对齐
<table>
<thead>
<tr>
<th>First Column</th>
<th>Second Column</th>
<th>Third Column</th>
</tr>
</thead>
<tbody>
<tr>
<td>Value One</td>
<td align="right">Value Two</td>
<td align="center">Value Three</td>
</tr>
<!-- more rows -->
</tbody>
</table>
在这个例子中,第二<th>
会得到一致的权利和第三<th>
会得到居中。第一个<th>
不会受到影响。任何建议/如何用jQuery来实现这一点的例子将非常感激。
基于InfernalBadger的回答,这是我最终使用的。原来我需要考虑CSS text-align
属性以及处理多个表。
function alignGridHeaders() {
$('table.grid thead th').each(function (index) {
var cell = $(this).parents('table:first').children('tbody tr:first td').eq(index);
var alignment = cell.css('text-align');
if (!alignment)
alignment = cell.attr('align');
if (alignment)
$(this).css('text-align', alignment);
});
}