Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.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
Jquery 如何将数据表标题居中对齐_Jquery_Html_Css_Datatables - Fatal编程技术网

Jquery 如何将数据表标题居中对齐

Jquery 如何将数据表标题居中对齐,jquery,html,css,datatables,Jquery,Html,Css,Datatables,我不熟悉数据表。当我制作表格标题时,它总是左对齐。 如何将收割台设置为居中对齐? 我读过datatables.net/manual/styleing/classes和datatables.net/reference/option/columns.className,但仍然不知道如何实现它 $('.table').DataTable({ "paging": true, "lengthChange": true, "searching": true, "ordering": true

我不熟悉数据表。当我制作表格标题时,它总是左对齐。 如何将收割台设置为居中对齐? 我读过datatables.net/manual/styleing/classes和datatables.net/reference/option/columns.className,但仍然不知道如何实现它

$('.table').DataTable({
  "paging": true,
  "lengthChange": true,
  "searching": true,
  "ordering": true,
  "info": true,
  "language": {
    "url": "http://cdn.datatables.net/plug-ins/1.10.9/i18n/Indonesian.json"
  }
  "columnDefs": {
    {
      className: "dt-head-center"
    }
  }
});

在指定类之后,您可能忘记了,您需要在CSS中添加以下内容:

.dt磁头中心{文本对齐:中心;}
此外,如果该类尚未添加到表的
,请尝试为泛型内容添加以下CSS:

thead,th{text align:center;}
/*或*/
.表thead,
.table th{text align:center;}
要使其特定于特定的表,您可以给该表一个
id=“tableID”
,然后在CSS中,您可以执行以下操作:

#tableID thead,
#tableID th{text align:center;}

您可以使用CSS实现这一点。只需将table类用作选择器,并将该选择器中的每个表标题作为目标,如下所示:

.table th {
  text-align: center;
}
通过使用此样式:

table.center-all td,th{
    text-align :center;
}
我可以将
center all
类添加到我的表中,所有内容都将与中心对齐。像这样:

<table class="center-all">
    ....
</table

....

OP,您非常接近解决方案,只是缺少了
columnDefs
中的
targets
选项,无法将
dt head center
类指定给要设置样式的标题

// apply to columns 1 & 2
targets: [ 0, 1 ]
CSS是一个选项,但不是必需的

我喜欢DataTables建议的使用
列.className
选项设置单元格样式的方法,然后使用
目标应用它们。与全局CSS应用程序相比,这为我们提供了更好的控制级别

这将分别对齐标题和正文内容:

columnDefs: [
    // Center align the header content of column 1
   { className: "dt-head-center", targets: [ 0 ] },
   // Center align the body content of columns 2, 3, & 4
   { className: "dt-body-center", targets: [ 1, 2, 3 ] }
]
或同时将两者对齐:

columnDefs: [
   // Center align both header and body content of columns 1, 2 & 3
   { className: "dt-center", targets: [ 0, 1, 2 ] }
]
单元类格式:

dt[-head|-body][-left|-center|-right|-justify|-nowrap]

有关DataTables单元格类的更多信息:

您需要使用CSS。你能不能制作一个工作片段,这样我们就可以提出建议?我发现只要确保类在标题上就足够了。添加额外的css是不必要的,因为它已经存在于数据表中。