【发布时间】:2023-03-27 12:48:01
【问题描述】:
我使用 Print Media Query 在我的网页上打印一个可滚动的 DIV(主 DIV 包含一个子 DIV 和表格,其中包含来自剑道网格的多行和自定义样式)。 window.Print() 仅在 IE 9 和 Chrome 中打印一页,并切割 DIV 内容的其余部分。我将如何确保它在多个页面中打印所有内容。我阅读了有关 Firefox 问题的类似帖子,但使用 overflow: visible !important 的解决方案对我不起作用。下面是我的风格
注意:我尝试过使用 position: absolute, height/width: 100% 并为 Table、TBody、TR 和 TD 设置如下相同的设置,但没有用。
@media print {
body * {
visibility: hidden;
}
#divname, #divname* {
visibility: visible;
}
#divname
{
overflow: visible !important;
float:none !important;
position: fixed;
left: 0px;
top: 0px;
display:block !important;
/*height:auto !important;*/
}
}
编辑:我终于设法通过从 DOM 中读取来打印,如下所示。以防万一,如果它对某人有帮助
`//get DIV content as clone
var divContents = $("#DIVNAME").clone();
//detatch DOM body
var body = $("body").detach();
//create new body to hold just the DIV contents
document.body = document.createElement("body");
//add DIV content to body
divContents.appendTo($("body"));
//print body
window.print();
//remove body with DIV content
$("html body").remove();
//attach original body
body.appendTo($("html"));`
这样,您可以在重新绑定后保留与页面上的控件关联的客户端事件。
【问题讨论】:
-
我想出了一个替代解决方案,它只是打开一个新窗口,将 HTML 填充到其中,然后打印出来。对我来说很好。