首页 > 解决方案 > Javascript函数中的多个ID

问题描述

我有多个具有不同 ID 标签的数据表,如果存在多个 ID,我正在尝试让 JavaScript 文件应用样式。

这是我所拥有的不起作用的:

$(function() {
  $("[id*=tblAccount") || ("[id *= tblCustomer") || ("[id *= tblContact").prepend($("<thead></thead>").append($(this).find("tr:first"))).DataTable({
    "paging": true,
    "lengthChange": true,
    "searching": true,
    "ordering": true,
    "info": true,
    "autoWidth": false,
    "dom": 'lBfrtip',
    "buttons": ['excel', 'print', 'pdfHtml5']
  });
})

标签: javascript

解决方案


您需要使用 CSS 选择器运算符在对 jQuery 的一次调用中组合选择器,

$("[id*=tblAccount], [id *= tblCustomer], [id *= tblContact]").prepend($("<thead></thead>").append($(this).find("tr:first"))).DataTable({

(另请注意,我添加了缺少的]字符。)


推荐阅读