簡體   English   中英

如何刪除用於打印的iFrame DOM節點

[英]How to remove the iFrame DOM node that is used for printing

我正在動態創建一個iFrame DOM,僅用於打印。 這是我的代碼,用於創建和顯示打印窗口。 var url,data,_iFrame,nonce,iframeId;

data = new Blob([buffer], {
   type: 'application/pdf'
});
url = window.URL.createObjectURL(data);
_iFrame = document.createElement('iframe');
nonce = (new Date()).getTime();
var iframeId = "printPDF" + nonce;
_iFrame.id = iframeId
_iFrame.setAttribute('style', 'visibility:hidden;');
_iFrame.setAttribute('src', url);
document.body.appendChild(_iFrame);

$('#' + iframeId)[0].focus();
$('#' + iframeId)[0].contentWindow.print();

但問題是,我需要在完成打印(打印或取消)后刪除iFrame。 如何使用javascript獲取事件?

我想像這樣的事情將為您完成工作:

$('#' + iframeId)[0].contentWindow.print();
setTimeout(function () { $('#' + iframeId)[0].contentWindow.close(); }, 100);

我實際上是自己解決的。

首先是不要隱藏iframe。 而是使其大小為0。 style='height: 0;width: 0;'

然后,綁定iframe的focus事件,並在關閉打印對話框后再次將iframe聚焦后將其移除。

好吧,我嘗試了焦點事件,但由於iframe被隱藏,所以未能參加。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM