javascript - 样式在第二张表中不起作用

标签 javascript html css

我在页面上使用的表格有点问题。

我为 <th> 定义的样式在我的第一个表中工作,但在第二个表中它不工作。

<th style="width:10px">Number</th>对于 table employee_grid1有一个 width:10px . 同<th>employee_grid2更大。

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<div class="" role="tabpanel" data-example-id="togglable-tabs">
  <ul id="myTab" class="nav nav-tabs bar_tabs" role="tablist">
    <li role="presentation" class="active"><a href="#tab_content1" id="home-tab" role="tab" data-toggle="tab" aria-expanded="true">1</a>
    </li>
    <li role="presentation" class=""><a href="#tab_content2" role="tab" id="profile-tab" data-toggle="tab" aria-expanded="false">2</a>
    </li>
  </ul>
  <div id="myTabContent" class="tab-content">
    <div role="tabpanel" class="tab-pane fade active in" id="tab_content1" aria-labelledby="home-tab">
      <table id="employee_grid1" class="display" width="100%" cellspacing="0">
        <thead>
          <tr>
            <th style="width:10px">Number</th>
            <th style="width:50px">Name</th>
            <th>Date</th>
            <th style="width:100px"></th>
          </tr>
        </thead>
      </table>
    </div>
    <div role="tabpanel" class="tab-pane fade" id="tab_content2" aria-labelledby="profile-tab">
      <table id="employee_grid2" class="display" width="100%" cellspacing="0">
        <thead>
          <tr>
            <th style="width:10px">Number</th>
            <th style="width:50px">Name</th>
            <th>Date</th>
            <th style="width:100px"></th>
          </tr>
        </thead>
      </table>
    </div>
  </div>
</div>

也许它是解决方案所必需的。该表由以下 javascript 填充:

<script type="text/javascript">
  $( document ).ready(function() {
    $('#employee_grid1').DataTable({
      "bprocessing": true,
      "serverSide": true,
      "ajax": {
        "url": "response1.php",
        "type": "POST",
        "error": function(){
          $("#employee_grid_processing").css("display","none");
        }
      },
      "columnDefs": [ 
        { "targets": 3, "render": function ( data, type, full, meta ) { return  '<p>'+data+'</p>'; } }              
      ]                 
    });   
  });
</script>

<script type="text/javascript">
  $( document ).ready(function() {
    $('#employee_grid2').DataTable({
      "bprocessing": true,
      "serverSide": true,
      "ajax": {
        "url": "response2.php",
        "type": "POST",
        "error": function(){
          $("#employee_grid_processing").css("display","none");
        }
      },
      "columnDefs": [ 
        { "targets": 3, "render": function ( data, type, full, meta ) { return  '<p>'+data+'</p>'; } }              
      ]                 
    });   
  });
</script>

最佳答案

在您的代码中,表格宽度为 100%,因此您最好在表格单元格上使用 %。示例:

<table width="100%" cellspacing="0">
<thead>
  <tr>
    <th width="10%">Number</th>
    <th width="30%">Name</th>
    <th>Date</th>
    <th width="60%"></th>
  </tr>
</thead>
</table>

或者,如果您希望表格宽度应为 100% 并且单元格应为固定宽度,则对表格使用“table-layout:fixed”和“width:100%”样式。

<table class="tableClass" cellspacing="0">
<thead>
  <tr>
    <th>Number</th>
    <th>Name</th>
    <th>Date</th>
    <th>Test</th>
  </tr>
</thead>
</table>

并添加如下样式

tableClass {  
  table-layout: fixed; 
  width:100% !important; 
}
th:nth-child(1){
  width:100px !important;
}
th:nth-child(2){
  width:150px !important;
}
th:nth-child(3){
  width:200px !important;
}
th:nth-child(4){
  width:100px !important;
}

关于javascript - 样式在第二张表中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44692536/

相关文章:

javascript - Handlebars 单引号问题

javascript - 我的网站模板是否包含内置搜索功能?

python - 如何在html中提取python列表变量的值?

html - 工具提示未正确显示

css - 使图像不在小型显示器上环绕

javascript - 如何为日期选择器字段着色?

javascript - 在 D3.js 中的 Bundle 布局中读取 JSON 文件时出错

javascript - 如何配置 apache 以使用 php 创建 javascript 文件

javascript - 进度条动画

jquery - 在不知道背景高度的情况下设置带背景的div高度