javascript - CSS 表 : How to position action buttons left to each row

标签 javascript jquery html css twitter-bootstrap

我有这个默认的响应式 Bootstrap 表:

<div class="table-responsive">
    <table class="table table-bordered">
      <thead>
        <tr>
          <th>#</th>
          <th>First Name</th>
          <th>Last Name</th>
          <th>Username</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <th scope="row">1</th>
          <td>Mark</td>
          <td>Otto</td>
          <td>@mdo</td>
        </tr>
        <tr>
          <th scope="row">2</th>
          <td>Jacob</td>
          <td>Thornton</td>
          <td>@fat</td>
        </tr>
        <tr>
          <th scope="row">3</th>
          <td>Larry</td>
          <td>the Bird</td>
          <td>@twitter</td>
        </tr>
      </tbody>
    </table>
</div>

如何在每行的右侧添加一个 div 或容器来显示一些操作按钮,如删除行、更新图标等?它们应该默认隐藏。当我将鼠标悬停在一行上时,它应该显示在该行的右边。不在表格内部,但顶部和底部应与给定表格行的位置和高度对齐。

enter image description here

我该如何解决这个问题?如果它不能单独使用 CSS 来完成,那么使用 jQuery/JavaScript 或类似的解决方案可能没问题。

最佳答案

试试下面的代码

$('tbody tr').hover(function(){
 $(this).find('td:last').show();
},function(){
  $(this).find('td:last').hide();
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<style>
  .table-bordered-custom{
    border:0px;
    border-top:initial;
  }
  
  .table-bordered-custom thead th {
    border-top:1px solid #ddd !important;
  }
  .table-bordered-custom tbody td:last-child {
    border:0 !important;
    display:none;
  }
  </style>
<table style="margin:10px" class="table table-bordered table-bordered-custom">
  
      <thead>
        <tr>
          <th width="10%">#</th>
          <th width="25%">First Name</th>
          <th width="25%">Last Name</th>
          <th width="25%">Username</th>
          <th style="border:0 !important" ></th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <th scope="row">1</th>
          <td>Mark</td>
          <td>Otto</td>
          <td>@mdo</td>
          <td ><input type="button" value="X"> <input type="button" value="Edit"></td>
          
        </tr>
        <tr>
          <th scope="row">2</th>
          <td>Jacob</td>
          <td>Thornton</td>
          <td>@fat</td>
          <td ><input type="button" value="X"> <input type="button" value="Edit"></td>
        </tr>
        <tr>
          <th scope="row">3</th>
          <td>Larry</td>
          <td>the Bird</td>
          <td>@twitter</td>
          <td ><input type="button" value="X"> <input type="button" value="Edit"></td>
        </tr>
      </tbody>
    </table>

关于javascript - CSS 表 : How to position action buttons left to each row,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28942949/

相关文章:

javascript - jquery-ui datepicker 没有出现在动态输入中

javascript - 是否可以使用 HTML 或 javascript 操作文本区域的各个行?

javascript - 可调整大小的 div 不响应百分比高度

Jquery加载刷新div

html - 你如何摆脱 svg 中的这些小透明像素线?

jquery - 空白 nowrap 不适用于 div 下面的 div,以及如何获取溢出的实际宽度

javascript - javascript中关联键和数字的数组组合

javascript - 如何使用 Jquery 根据 html 中的关键字为元素的背景着色

javascript - js 无法在响应式网站中运行的问题

javascript - Bootstrap数字输入旋转器不会停止旋转