简体   繁体   English

Javascript-表标题中心对齐

[英]Javascript - table header center align

I am creating a table as follows 我正在创建一个表,如下所示

table = document.createElement('table');
table.style.textAlign = 'center';
table.setAttribute('border', '2');

var thead = document.createElement('thead');
thead.style.color = "fuchsia ";
thead.style.textAlign = 'center';

When I populate the table, the row content is center aligned as expected but the header row is always left aligned. 当我填充表格时,行内容按预期居中对齐,但标题行始终保持左对齐。 What can I do to make it center aligned? 我该怎么做才能使其居中对齐?

Thanks 谢谢

thead is a container for th 's. theadth的容器。 The th 's always need to fill the thead and so the natural place to centre align things for a header row is within the th 's, so... th总是需要填充thead ,因此使标题行的内容居中对齐的自然位置就在th的内部,所以...

var th = document.createElement('th');
th.style.color = "fuchsia ";
th.style.textAlign = 'center';

Oh, and also, your thead hasn't got the centre alignment set, you're setting it on the table twice. 哦,而且您的 thead还没有设置中心对齐方式,因此您要在桌子上设置两次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM