![](/img/trans.png)
[英]How to open iframe in another iframe from different/external html file
[英]How to import a whole different HTML file into an iframe?
我有一个HTML文档,我想将其加载到iframe中,然后将打印预览弹出到Google chrome中。 这是我到目前为止所做的,但似乎没有用:
<script>
...
jQuery(document).ready(function($) {
$('#printerButton').click(function(){
openPrintDialogue();
});
});
function openPrintDialogue(){
$('<iframe>', {
id: 'frame',
scr: 'path/to/my/external/html/file',
name: 'myiframe',
class: 'printFrame'
})
.appendTo('body');
window.frames['myiframe'].focus();
window.frames['myiframe'].print();
setTimeout(() => { $(".printFrame").remove(); }, 1000);
};
...
</script>
<html>
...
<input type="button" id="printerButton" name="print" value="Print It" />
...
</html>
iframe会弹出,但它是空白的,我希望将其与我的外部html文件中的内容一起填充/归档。
注意:我这样做的主要原因是因为我要在按钮单击时打印外部html文件。 如果您知道任何比我做的事简单的方法,请通知我。
谢谢。
更新:解决。 谢谢perfect5th!
正如有人已经告诉您的那样-“ scr”应该是“ src”,但是您还有另一个问题:
setTimeout(() => { $(".printFrame").remove(); }, 1000);
在关闭打印窗口之前不会调用。 这是Chrome浏览器的限制。.(打印窗口将JS停止在父窗口中运行)。
而且无论如何,我会使用id选择器而不是类选择器来获取iframe。
setTimeout(() => { $("#frame").remove(); }, 1000);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.