2016-04-15 169 views
0

我想打印iframe的內容。我查看了幾個例子,但其中沒有一個例子幫助我解決了這個問題。我有以下的代碼打印iframe內容

<div id="print-area"> 
    <iframe src='data:text/html;charset=utf-8,<?php echo $content ?>'></iframe> 
</div> 

而且#PRINT單擊按鈕時:(此代碼打印出來的DIV#打印區域的含量)

$("#print").click(function (e) { 
     var newWindow = window.open(); 
     newWindow.document.write($('#print-area').html()); 
     newWindow.print() 
    }); 

代碼工作和打印提示窗口,但是當我嘗試將iframe的內容放入document.write(...)中時,該功能不起作用。任何想法?

試圖解決方案:

Print iFrame content using Javascript

How to Print iframe content

和其他議題,但沒能打印我的iframe中的內容

回答

1

使用

<iframe id="abcd" src='data:text/html;charset=utf-8,<?php echo "someHtmlContent" ?>'> 


      </iframe> 
var newWindow = window.open(); 
       newWindow.document.write($('#abcd').contents().find("html").html()); 
       newWindow.print() 

因爲iframe是一個獨立的p年齡它不能訪問Windows類 ,爲什麼使用父類

+0

我不能這樣做......可能不知道如何。你可以給我舉例 – Ivo

+0

如果你有任何演示鏈接,然後與我分享我會解決你的問題。 – Rajiv

+0

https://jsfiddle.net/r0m01dba/ – Ivo