【发布时间】:2013-08-29 16:59:12
【问题描述】:
问题是新内容不会打印出来(多了几个元素)。
当用户单击我的打印链接时,我会在调用 window.print() 之前向文档添加更多 html。
我在打印之前使用 ajax 为一本书获取更多章节。
代码:
打印初始化:
var afterPrint = function () {
var timer = setInterval(function () {
afterContentPrint(); // Cleanup html (restore to initial state)
clearInterval(timer);
}, 900);
}
//window.onbeforeprint = beforePrint;
window.onafterprint = afterPrint;
事件打印点击:
$("#print-test").click(function (e) {
e.preventDefault();
beforeContentPrint(); // ajax call for additional content, finishing by calling window.print()
});
在函数 beforeContentPrint() 中:
var jqxhr = $.ajax({
type: "GET",
url: bookURL,
success: function(data) {
.....
.....
$(article).each(function () {
parent.append(article);
});
},
complete: function() {
var timer = setInterval(function () {
window.print();
}, 900);
}
}
新内容明显添加到 HTML 文档中,因此它应该可以工作。但只有初始内容(在 ajax 调用之前)被提取出来进行打印。
此解决方案适用于 IE 和 Firefox(onbeforeprint 和 onafterprint)。
使用 window.matchMedia('print') 在 Chrome 中使用这种逻辑似乎可以正常工作。
【问题讨论】:
-
打印预览或通过Ctrl+P打印是否输出所有页面?
标签: javascript jquery html ajax printing