I came across another elegant solution for this:
(我遇到了另一个优雅的解决方案:)
Place your printable part inside a div with an id like this:
(将您的可打印部分放在一个id为div的div中:)
<div id="printableArea">
<h1>Print me</h1>
</div>
<input type="button" onclick="printDiv('printableArea')" value="print a div!" />
Now let's create a really simple javascript:
(现在让我们创建一个非常简单的javascript:)
function printDiv(divName) {
var printContents = document.getElementById(divName).innerHTML;
var originalContents = document.body.innerHTML;
document.body.innerHTML = printContents;
window.print();
document.body.innerHTML = originalContents;
}
SOURCE : SO Answer
(消息来源: SO答案)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…