简体   繁体   English

打印后清除网页

[英]Clear webpage after printing

I am trying to print the content of a particular DIV and after printing the DIV the webpage has to be fixed properly but its just displaying only the DIV I just printed and hides the actual webpage. 我正在尝试打印特定DIV的内容,并且在打印DIV之后必须正确修复网页,但它仅显示我刚刚打印的DIV并隐藏实际的网页。 Any guess what's wrong? 猜猜怎么了?

My code is 我的代码是

function printdiv(printpage)
{
var newstr = document.all.item(printpage).innerHTML;
document.body.innerHTML = newstr;
window.print();
window.close();
}

and

<img src="im/print.png" onClick="printdiv('mytabprint');" width="50" />

In the above code mytabprint is the DIV element which I want to print. 在上面的代码中, mytabprint是我要打印的DIV元素。

Any guess what's wrong? 猜猜怎么了?

Yes, you're not "hiding" the other contents, you're actually replacing the whole page contents with your div: 是的,您不是在“隐藏”其他内容,实际上是将整个页面内容替换为div:

document.body.innerHTML = newstr;

You can simply use a print stylesheet instead. 您可以简单地使用打印样式表来代替。 For example, add this to your CSS: 例如,将其添加到CSS:

@media print {
   /* 
   rules that only apply for printing 
   like hiding some content
   */
}

Or link a separate CSS for printing with 或链接单独的CSS进行打印

<link rel="stylesheet" href="your_css.css" type="text/css" media="print" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM