【问题标题】:How do I hide a table column in print mode?如何在打印模式下隐藏表格列?
【发布时间】:2018-08-30 00:27:33
【问题描述】:

如何在打印模式下隐藏整个表格列(使用 JS javascript:print())?

因为我使用的是 Bootstrap,所以我尝试了它的 .hidden-print 类在打印模式下隐藏最后一列(操作列):

<table class="table table-striped">
  <thead>
    <tr>
      <th>No</th>
      <th>Name</th>
      <th class="hidden-print">Operation</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1</td>
      <td>John Smith</td>
      <td class="hidden-print">
        <a href="edit.php" role="button" class="btn">Edit</a>
        <a href="delete.php" role="button" class="btn">Delete</a>
      </td>
    </tr>
    <tr>
      <td>2</td>
      <td>Neo</td>
      <td class="hidden-print">
        <a href="edit.php" role="button" class="btn">Edit</a>
        <a href="delete.php" role="button" class="btn">Delete</a>
      </td>
    </tr>
    <tr>
      <-- continued -->
    </tr>
  </tbody>
</table>

但它只是隐藏列的内容,显示没有内容的列,当我需要时它还隐藏了 TH 和 TD 标签。

可以这样做吗?

【问题讨论】:

    标签: css twitter-bootstrap-3


    【解决方案1】:

    table,th,td {
      border: 1px solid;
      text-align: center;
    }
    table {
      border-collapse: collapse;
      width: 100%;
    }
    @media print {
      table,th,td {
        border: 0px
      }
      button {
        display: none;
      }
    }
    <table class="table table-striped">
      <thead>
        <tr>
          <th>No</th>
          <th>Name</th>
          <th class="hidden-print">Operation</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>1</td>
          <td>John Smith</td>
          <td class="hidden-print">
            <a href="edit.php" role="button" class="btn">Edit</a>
            <a href="delete.php" role="button" class="btn">Delete</a>
          </td>
        </tr>
        <tr>
          <td>2</td>
          <td>Neo</td>
          <td class="hidden-print">
            <a href="edit.php" role="button" class="btn">Edit</a>
            <a href="delete.php" role="button" class="btn">Delete</a>
          </td>
        </tr>
        <tr>
          
        </tr>
      </tbody>
    </table>
    <button onclick="window.print();">Print</button>

    【讨论】:

    • 这是一个很好的解决方案,但它对我不起作用,因为表格单元格的内容正在居中,这不是我的意图。还是谢谢。
    • 你可以改变" text-align: left; "
    【解决方案2】:

    display: none;.hidden-print 所做的事情)添加到表格行和单元格存在问题,因为从文档流中删除它们很容易弄乱表格格式 (see this question)。

    改用visibility: hidden; 怎么样?

    在您的情况下,如果您想始终以最后一列为目标,您可以试试这个:

    @media print {
      th:last-child, td:last-child {
        visibility: hidden;
      }
    }
    

    【讨论】:

    • 对不起,这和我原来的代码一样:只隐藏内容,边框等还在。
    • 奇怪,这对我有用,即使在 &lt;tr&gt;&lt;td&gt; 元素上设置了背景和边框。不过,它不适用于调整整个表格的边框。
    猜你喜欢
    • 1970-01-01
    • 2019-01-04
    • 2012-08-22
    • 2015-02-21
    • 2021-05-09
    • 2010-09-21
    • 1970-01-01
    • 2014-12-11
    相关资源
    最近更新 更多