【问题标题】:Print only certain part of the <div> content仅打印 <div> 内容的特定部分
【发布时间】:2017-07-29 18:13:29
【问题描述】:

我有以下 JavaScript 代码来打印内容

<script type="text/javascript">

    function Print(print)
    {
        Popup($(print).html());
    }

    function Popup(data) 
    {
        var mywindow = window.open("", "print");
        mywindow.document.write("<html><head><title>Report</title>");
        mywindow.document.write("</head><body>");
        mywindow.document.write("<div align='center'>");
        mywindow.document.write(data);
        mywindow.document.write("</div>");
        mywindow.document.write("</body></html>");

        mywindow.print();
        mywindow.close();

        return true;    }

</script>

在我的 HTML 中,我有以下代码:

   <html>
    <body>
    <p><a href="javascript:Print('#print')">Print</a></p>
    <div id="print">
    <h1>Report</h1>
    <table>
    <tr><th>Date</th><th>Remarks</th><th>Amount</th><th>Actions</th></tr>
    <tr>
    <td>10/11/2014</td>
    <td>Hello</td>
    <td>$14</td>
    <td>Edit</td>
    </tr>
    </table>
    </div>
</body>
</html>

我只想打印前 3 列。我不想打印第 4 列“操作”。当我尝试使用上面的代码打印出内容时,它会打印所有 4 列。请帮我解决以下问题。

【问题讨论】:

    标签: javascript html mysql printing html-table


    【解决方案1】:

    您可以简单地将样式设置为不显示该 td 的 id

    HTML 内部:

    ....
    <th id="act">Actions</th>
    ....
    <td id="act">Edit</td>
    

    JS 内部:

    mywindow.document.write("<html><head><style>#act{display:none;}</style><title>Report</title>");
    

    【讨论】:

      【解决方案2】:

      你可以用 jquery 做到这一点。

      $(document).ready(function(){
          $('.tabel').find(':nth-child(4)').hide();
      });
      

      JSFIDDLE HERE

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-16
        • 1970-01-01
        • 2021-09-13
        • 2018-06-24
        • 1970-01-01
        相关资源
        最近更新 更多