我正在尝试打印特定 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元素。
最佳答案
Any guess what's wrong?
是的,您没有“隐藏”其他内容,您实际上是用您的 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" />
关于javascript - 打印后清除网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17171645/