javascript - 表头和表体列不匹配

标签 javascript html css twitter-bootstrap

我已经创建了一个 Bootstrap 表,其中的行将被动态添加。但是表体和表头对齐不匹配,如下面的代码片段所示。

function display() {


  var tableRef = document.getElementById('myTable1').getElementsByTagName('tbody')[0];
  var rowsAdd = tableRef.insertRow(tableRef.rows.length);

  //    var m = moment().format('YYYY-MM-DD h:mm a');

  var newCell = rowsAdd.insertCell();
  newCell.innerHTML = "<input type='text'  form ='form1'  class= 'form-control input-sm' value=' ' id = 'typeofdr' name= 'typeofdr'  required>";
  newCell.style.width = '100px';

  newCell = rowsAdd.insertCell();
  newCell.innerHTML = "<tr><td><input  form ='form1' class= 'form-control input-sm' value=' '  type='text' id = 'drugname' name= 'drugname'  required> </td></tr>";
  newCell.style.width = '100px';

  newCell = rowsAdd.insertCell();
  newCell.innerHTML = "<tr><td><input  form ='form1' class= 'form-control input-sm' value=' ' type='text' id = 'strdrug' name= 'strdrug'  required> </td></tr>";
  newCell.style.width = '100px';

  newCell = rowsAdd.insertCell();
  newCell.innerHTML = "<tr><td><input  form ='form1' class= 'form-control input-sm' value=' ' type='text' id = 'dosage' name= 'dosage'  required> </td></tr>";
  newCell.style.width = '50px';

  newCell = rowsAdd.insertCell();
  newCell.innerHTML = "<tr><td><input  form ='form1' class= ''  type='checkbox' id = 'dm' value= 'on' name= 'dm'  ><input type='hidden' name='dm' value='off' form='form1'> </td></tr>";
  newCell.style.width = '37.5px';

  newCell = rowsAdd.insertCell();
  newCell.innerHTML = "<tr><td><input  form ='form1' class= ''  type='checkbox' id = 'da' name= 'da' value='on'  ><input type='hidden' name='da' value='off' form='form1'> </td></tr>";
  newCell.style.width = '37.5px';

  newCell = rowsAdd.insertCell();
  newCell.innerHTML = "<tr><td><input  form ='form1' class= ''  type='checkbox' id = 'de' name= 'de' value='on' ><input type='hidden' name='de' value='off' form='form1'> </td></tr>";
  newCell.style.width = '37.5px';

  newCell = rowsAdd.insertCell();
  newCell.innerHTML = "<tr><td><input  form ='form1' class= ''  type='checkbox' id = 'dn' name= 'dn' value='on' ><input type='hidden' name='dn' value='off' form='form1'> </td></tr>";
  newCell.style.width = '37.5px';

  newCell = rowsAdd.insertCell();
  newCell.innerHTML = "<tr><td><select  form ='form1' class= 'form-control input-sm' value=' ' id = 'baf' name= 'baf'  required><option>Before</option><option>After</option></select> </td></tr>";
  newCell.style.width = '90px';

  newCell = rowsAdd.insertCell();
  newCell.innerHTML = "<tr><td><input  form ='form1' class= 'form-control input-sm' value=' ' type='text' id = 'totn' name= 'totn'  required> </td></tr>";
  newCell.style.width = '70px';

  newCell = rowsAdd.insertCell();
  newCell.innerHTML = "<tr><td><input  form ='form1' class= 'form-control input-sm' value=' ' type='text' id = 'nofdays' name= 'nofdays'  required> </td></tr>";
  newCell.style.width = '50px';



  newCell = rowsAdd.insertCell();
  newCell.innerHTML = "<tr><td class='tds'><i class='fa fa-trash-o' font-size:20px'  onclick='deleteRow(this)'></i></td></tr>";
  newCell.style.width = '50px';



}

function deleteRow(r) {

  var i = r.parentNode.parentNode.rowIndex;
  document.getElementById("myTable1").deleteRow(i);


}
table.alpha th {
  background-color: #009999;
  color: white;
}

table.alpha .tbalpha {
  height: 200px;
  overflow-y: auto;
}

table.alpha .thalpha,
.tbalpha {
  display: block;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<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="container">




  <table class="table table-striped table-bordered table-responsive table-hover table-condensed alpha" style="width: 760px; " id="myTable1">
    <thead class="thalpha">
      <tr>
        <th rowspan="2" style="width:100px;">Item</th>
        <th rowspan="2" style="width:100px;">Name of Drug</th>
        <th rowspan="2" style="width:100px;">Strength of Drug</th>
        <th rowspan="2" style="width:50px;">Dosage</th>
        <th colspan="4" style="width:150px;">Frequency</th>
        <th rowspan="2" style="width:90px;">Before /After Food</th>
        <th rowspan="2" style="width:70px;">Total No. of Tablets to be dispensed</th>
        <th rowspan="2" style="width:50px;">No. of Days</th>
        <th rowspan="2" style="width:30px;">Delete Row</th>
      </tr>
      <tr>
        <th style="width:37.5px;">M</th>
        <th style="width:37.5px;">A</th>
        <th style="width:37.5px;">E</th>
        <th style="width:37.5px;">N</th>
      </tr>
    </thead>

    <tbody class="tbalpha">


    </tbody>

    <tr id="hide">
      <td><i class='fa fa-plus' style='font-size:20px; color : #ff9900;' onclick="display()"></i></td>
    </tr>

  </table>




</div>

如上面的代码片段所示,表格列标题和表格主体列行都没有正确对齐,即表格单元格不在其相应标题单元格的正下方。我猜 css 可能有问题,但如果在 css 中删除 display:block,即使设置了高度(在上面的 css 代码中 tbody 高度设置为 200px),表体高度也会降低。

最佳答案

那是因为您正在改变 tbodythead 的显示方式:

table.alpha .thalpha, .tbalpha {
    display: block;
}

如果删除它,列将对齐

关于javascript - 表头和表体列不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47154216/

相关文章:

javascript - YouTube 数据 API 搜索中的 'items' 元素在调用 '/videos' 时不返回任何内容

html - 左、中、右文本

javascript - 为什么 vanilla HTML/JS(和 React)使用驼峰式命名,而 CSS 不使用?

javascript - 为什么 bootstrap 无法访问在单击 _Layout.cshtml 的选项卡上获得的 View ?

html - 如何为嵌入的YouTube视频添加边框半径?

javascript - Django,如何强制返回ajax错误?

javascript - 仅使用 JS 检测 net::ERR_FILE_NOT_FOUND

javascript - 如何向 selectize.js 添加/删除类?

javascript - 背景网格上每个 block 的不同图像

javascript - 在表最后一行(链接)显示菜单并在单击菜单项时显示模态窗口