javascript - Bootstrap 标题和主体的行在 Bootstrap 响应表中未正确对齐

标签 javascript jquery html css twitter-bootstrap

我在这里创建了一个响应式 Bootstrap 表,我想在其中动态添加我正在使用 jquery 的行。但是表体行没有与其各自的标题正确对齐,如下所示

$(document).ready(function() {
  var rows = "";
  var a = "test code";

  $("#add_row").click(function() {

    rows += "<tr><td>" + a + "</td><td>" + a + "</td><td>" + a + "</td><td>" + a + "</td><td>" + a + "</td><td>" + a + "</td><td>" + a + "</td><td>" + a + "</td><td>" + a + "</td><td>" + a + "</td></tr>";
    $(rows).appendTo("#myTable3 tbody");
  });
});
#myTable3 th {
  background-color: #009999;
  color: white;
}

.table-fixed {}

#myTable3 tbody {
  height: 100px;
  overflow: auto;
}

#myTable3 thead,
.tbody {
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<div class="table-responsive">
  <div class="row clearfix">

    <table class="table table-striped table-bordered table-fixed table-hover table-condensed" style="border:0px; " id="myTable3">
      <thead>
        <tr>
          <th width="">FileNo</th>
          <th width="">Height</th>
          <th width="">Weight</th>
          <th width="">Temperature</th>
          <th width="">Abdominal Circumference</th>
          <th width="">Blood Pressure</th>
          <th width="">Pulse</th>
          <th width="">BMI</th>
          <th width="">Chest Circumference</th>
          <th width="">Time Recorded</th>

        </tr>
      </thead>
      <tbody class="tbody">

      </tbody>
    </table>
    <a id="add_row" class="btn btn-default pull-left">Add Row</a>
  </div>
</div>

以上是解释上述问题的片段。但是我已经使用了 thead 和 tbody 的显示 block 来获得 tbody 的滚动条。如果我删除 css 中的显示 block 部分,tbody 和 thead 的对齐工作正常,但我需要为 tbody 滚动。

请帮帮我 谢谢

最佳答案

问题是您将 tbody 显示为 block,这是错误的,只需将其删除,也无需将表格包装在 中。 row, .table-responsive 就够了。

#myTable3 thead,
.tbody {
  /*display: block;*/
}

$(document).ready(function() {
  var rows = "";
  var a = "test code";

  $("#add_row").click(function() {

    rows += "<tr><td>" + a + "</td><td>" + a + "</td><td>" + a + "</td><td>" + a + "</td><td>" + a + "</td><td>" + a + "</td><td>" + a + "</td><td>" + a + "</td><td>" + a + "</td><td>" + a + "</td></tr>";
    $(rows).appendTo("#myTable3 tbody");
  });
});
#myTable3 th {
  background-color: #009999;
  color: white;
}

.table-fixed {}

#myTable3 tbody {
  height: 100px;
  overflow: auto;
}

#myTable3 thead,
.tbody {
  /*display: block;*/
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<div class="table-responsive">

    <table class="table table-striped table-bordered table-fixed table-hover table-condensed" style="border:0px; " id="myTable3">
      <thead>
        <tr>
          <th width="">FileNo</th>
          <th width="">Height</th>
          <th width="">Weight</th>
          <th width="">Temperature</th>
          <th width="">Abdominal Circumference</th>
          <th width="">Blood Pressure</th>
          <th width="">Pulse</th>
          <th width="">BMI</th>
          <th width="">Chest Circumference</th>
          <th width="">Time Recorded</th>

        </tr>
      </thead>
      <tbody class="tbody">

      </tbody>
    </table>
    <a id="add_row" class="btn btn-default pull-left">Add Row</a>
</div>

关于javascript - Bootstrap 标题和主体的行在 Bootstrap 响应表中未正确对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47121753/

相关文章:

javascript - jQuery UI - 如何区分拖动/调整大小开始事件和单击事件?

javascript - 加快 Canvas 的 getImageData

javascript - 如何消除 div 和 span 之间的差距

javascript - 更改 DDL 中特定 <option> 的样式

java - jsoup java html解析

javascript - 在 Firebase Hosting 中哪里可以编辑 .htaccess 文件

javascript - 如何创建一个没有绝对位置的简单 jQuery 图像 slider

php - 搜索框可同时单击按钮 PHP Jquery

javascript - 如何使用浏览器窗口的大小?

html - 悬停下拉菜单在我点击它之前就消失了