您不应该替换整个页面的 html,因为这会删除所有单击处理程序(例如处理选项卡的处理程序)
要打印,您应该执行以下操作:
- 创建一个显示在所有内容上方的 div(固定,高度/宽度:100%,顶部/左侧:0)
- 隐藏身体中的一切
- 将要打印的内容添加到 div
- 来电打印
- 删除 div
- 恢复身体状态
就像是:
JS
function printReport() {
var $printerDiv = $('<div class="printContainer"></div>'); // create the div that will contain the stuff to be printed
$printerDiv.html(divElements); // add the content to be printed
$('body').append($printerDiv).addClass("printingContent"); // add the div to body, and make the body aware of printing (we apply a set of css styles to the body to hide its contents)
window.print(); // call print
$printerDiv.remove(); // remove the div
$('body').removeClass("printingContent");
}
CSS
body.printingContent > *{
display: none !important; /* hide everything in body when in print mode*/
}
.printContainer {
display: block !important; /* Override the rule above to only show the printables*/
position: fixed;
z-index: 99999;
top: 0;
left: 0;
width: 100%;
height: 100%;
}