打印时如何隐藏打印按钮? [英] How to hide print button while printing?

查看:396
本文介绍了打印时如何隐藏打印按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在按钮点击时打印页面而无需打开新标签。

I want to print the page on button click without opening a new tab.

我有以下几行代码可为我完成这项工作。

I have a following lines of code that does the job for me.

function PrintDiv() {    
    var contents = document.getElementById("wrapper").innerHTML;
    var frame1 = document.createElement('iframe');
    frame1.name = "frame1";
    frame1.style.position = "absolute";
    frame1.style.top = "-1000000px";
    document.body.appendChild(frame1);
    var frameDoc = frame1.contentWindow ? frame1.contentWindow : frame1.contentDocument.document ? frame1.contentDocument.document : frame1.contentDocument;
    frameDoc.document.open();
    frameDoc.document.write('<html><head><title>DIV Contents</title>');
    frameDoc.document.write('</head><body>');
    frameDoc.document.write(contents);
    frameDoc.document.write('</body></html>');
    frameDoc.document.close();
    setTimeout(function () {
        window.frames["frame1"].focus();
        window.frames["frame1"].print();
        document.body.removeChild(frame1);
    }, 500);

    return false;
}

我在div中使用id wrapper获得打印按钮

I got a print button inside the div with id wrapper

<div id="button-container">
    <a href="#" id="PrintDiv" class="btn btn-success btn-primary"><span class="glyphicon glyphicon-print" aria-hidden="true"></span>Print</a>
</div>

我的问题是我无法在打印时隐藏打印按钮。

My problem is I am not being able to hide the print button while printing.

我试过下面几行来隐藏它,但它在我的情况下不起作用。

I have tried following lines to hide but its not working in my case.

$('#PrintDiv').css('visibility', 'hidden');


推荐答案

您需要有基于打印的CSS样式规则 - 您还可以设置其他基于打印的规则 - 例如文档大小,背景颜色,图像规格等等:

You need to have a print based CSS style rule - you can also set other print based rules - such as document size, background color , image specs etc:

@media print {
   #button-container{
      display: none;
   }
}

这篇关于打印时如何隐藏打印按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆