【问题标题】:How to remove the iFrame DOM node that is used for printing如何删除用于打印的 iFrame DOM 节点
【发布时间】:2016-08-11 12:04:32
【问题描述】:

我正在动态创建 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 中获取事件?

【问题讨论】:

  • 不幸的是,afterprint 事件仅在 Firefox 和 IE 中可用......所以这是不行的

标签: javascript jquery iframe


【解决方案1】:

我想这样的事情会为你工作:

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

【讨论】:

    【解决方案2】:

    我实际上是自己解决了这个问题。

    首先不要隐藏 iframe。相反,将其设为 0 大小。 style='height: 0;width: 0;'.

    然后,绑定 iframe 的 focus 事件,并在打印对话框关闭后再次聚焦时移除 iframe。

    好吧,我尝试了焦点事件,但由于 iframe 被隐藏了,我无法进入它。

    【讨论】:

      猜你喜欢
      • 2016-05-12
      • 2016-03-31
      • 2015-03-26
      • 2021-05-30
      • 1970-01-01
      • 2012-06-30
      • 2014-06-10
      • 2012-07-29
      • 1970-01-01
      相关资源
      最近更新 更多