我正在尝试在点击时打印div内容,我可以打印它但在取消打印预览后,我的jQuery事件都没有工作。
为什么会这样?我打算在打印预览后刷新页面
这是我用于打印的JS代码
$('.btn li:nth-child(1) > a').on("click", function(e){
var divElements = document.getElementById("divContentId").innerHTML;
var oldPage = document.body.innerHTML;
document.body.innerHTML =
"<html><head><title>Print Title</title></head><body><table width='670'><tr><td><h1>This is Print Preview Title</h1>" + divElements + "</td></tr></table></body>";
setTimeout(function() {
window.print();
document.body.innerHTML = oldPage;
window.close();
return true;
}, 250);
$(".btn-options").hide(); // this is not firing after cancelling print preview event none of other events working
});
答案 0 :(得分:1)
正如我在评论中所说,您可以在print
内iframe
代替:
$('.test').on("click", function() {
var divElements = $("#divContentId").html();
$('<iframe class="hidden" id="printer"></iframe>').appendTo('body');
var printer = $('#printer');
printer.contents().find('body').append("<table width='670'><tr><td><h1>This is Print Preview Title</h1>" + divElements + "</td></tr></table>");
printer.get(0).contentWindow.print();
printer.remove();
//Do your things here ...
});
See this JSFiddle for a working example. (我无法在代码段中执行此操作)