我正在尝试打印特定DIV的内容,并且在打印DIV之后必须正确修复网页,但是它仅显示我刚刚打印的DIV并隐藏了实际的网页。你猜怎么了?

我的代码是

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




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


在上面的代码中,mytabprint是我要打印的DIV元素。

最佳答案

你猜怎么了?


是的,您不是在“隐藏”其他内容,实际上是用div替换了整个页面的内容:

document.body.innerHTML = newstr;


您可以简单地使用打印样式表。例如,将其添加到您的CSS中:

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


或链接单独的CSS进行打印

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

09-25 18:21
查看更多