Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript-表格标题中心对齐_Javascript_Html_Dom - Fatal编程技术网

Javascript-表格标题中心对齐

Javascript-表格标题中心对齐,javascript,html,dom,Javascript,Html,Dom,我正在创建一个表,如下所示 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'; 填充表格时,行内容按预期居

我正在创建一个表,如下所示

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';
填充表格时,行内容按预期居中对齐,但标题行始终左对齐。我该怎么做才能使它居中对齐


谢谢

thead
th
的容器。
th
始终需要填充
thead
,因此标题行居中对齐的自然位置在
th
内,因此

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

哦,还有,你的
thead
没有设置中心对齐,你在表格上设置了两次。

@oGeez实际上我是在js文件中动态创建表格的。我使用var resultDiv=document.getElementById(“resultDiv”);document.createElement('table')我知道,对不起,我匆忙阅读了。您需要在
thead
中包含行和单元格元素。Klors解决方案为我修复了它。谢谢,问题中有一个输入错误,我实际上是在标题上设置了textAlign。