簡體   English   中英

如何將整個HTML文件導入iframe?

[英]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.

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