我使用此代码,其源于here和here。Javascript/jQuery:删除iframe后contentWindow.print
$('#my_button').on('click', function (e) {
var iframe = document.createElement('iframe');
iframe.id = "my_iframe";
iframe.onload = function() {
var doc = iframe.contentDocument || iframe.contentWindow.document;
doc.getElementsByTagName('body')[0].innerHTML = "<p>test123</p>";
iframe.contentWindow.focus();
iframe.contentWindow.print();
$("#my_iframe", top.document).remove();
};
document.getElementsByTagName('body')[0].appendChild(iframe);
});
没有remove
行,它打印罚款。但是,在有机会执行print()
之前,remove
行将删除iframe
。我如何设置某种回调以便打印并仅删除iframe?
谢谢。
Upvoted,但也请复制粘贴+ window.onafterprint'代码从链接到您的答案。另外提到它在所有浏览器中都不支持。 – Stano 2012-08-15 08:47:48
完成 - 但阅读链接的文章以了解限制很重要。 – m7o 2012-08-15 08:55:17
感谢:) – Nick 2012-08-15 12:08:00