Remove print button from hard copy

不羁岁月 提交于 2019-12-02 17:52:55

问题


With reference to this link and this, I printing a report using javascript as

 <!DOCTYPE html>
<html>
<head>
<script>
function printpage()
{
var data = 'Sample Report<br />Sample Report<br />Sample Report<br />';
    var data = data+'<br/><button onclick="window.print()">Print the Report</button>';       
    myWindow=window.open('','','width=800,height=600');
    myWindow.innerWidth = screen.width;
    myWindow.innerHeight = screen.height;
    myWindow.screenX = 0;
    myWindow.screenY = 0;
    myWindow.document.write(data);
    myWindow.focus();
}
</script>
</head>
<body>

<input type="button" value="Print Preview" onclick="printpage()" />

</body>
</html>

But after printing, the print button still remains at the hard copy. So how to hide the print button in hard copy when printing by using the above function?


回答1:


Give your button a class, e.g. class="noprint". Then Add a stylesheet for print media to your CSS:

@media print {
  /* style sheet for print goes here */
  .noprint {
    visibility: hidden;
  }
}

Details: http://www.w3.org/TR/CSS2/media.html




回答2:


I just solved this with the following css after assigning id to print button.

<style type="text/css">
@media print {
    #myPrntbtn {
        display :  none;
    }
}
</style>

And my button as follows.

<input id ="myPrntbtn" type="button" value="Print" onclick="window.print();" >

Also, you can run javascript function with the following actions...

  1. Hide the button
  2. Print the page using window.print();
  3. Again Show back the button

Piece of code...

<script type="text/javascript">
    function printMyPage() {
        //Get the print button
        var printButton = document.getElementById("myPrntbtn");
        //Hide the print button 
        printButton.style.visibility = 'hidden';
        //Print the page content
        window.print()
        //Show back the print button on web page 
        printButton.style.visibility = 'visible';
    }
</script>

Reference Hide Print button while printing a web page



来源:https://stackoverflow.com/questions/16960806/remove-print-button-from-hard-copy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!