参考this link和this,我使用javascript打印报告
<!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>
但打印后,打印按钮仍保留在硬拷贝上。那么如何使用上述功能在打印时隐藏打印按钮?
答案 0 :(得分:6)
为您的按钮添加课程,例如class="noprint"
。然后将打印介质的样式表添加到CSS:
@media print {
/* style sheet for print goes here */
.noprint {
visibility: hidden;
}
}
答案 1 :(得分:0)
我在将id分配给打印按钮后,用以下css解决了这个问题。
<style type="text/css">
@media print {
#myPrntbtn {
display : none;
}
}
</style>
我的按钮如下。
<input id ="myPrntbtn" type="button" value="Print" onclick="window.print();" >
此外,您可以使用以下操作运行javascript函数...
一段代码......
<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>