我想打印 iframe 的内容。我查看了几个例子,但没有一个对我的案例有帮助。我有以下代码
<div id="print-area">
<iframe src='data:text/html;charset=utf-8,<?php echo $content ?>'></iframe>
</div>
当点击#print按钮时:(此代码打印出div #print-area的内容)
$("#print").click(function (e) {
var newWindow = window.open();
newWindow.document.write($('#print-area').html());
newWindow.print()
});
代码有效并提示打印窗口,但是当我尝试将 iframe 的内容放入 document.write(...) 时,该功能不起作用。有什么想法吗?
尝试过的解决方案:
Print iFrame content using Javascript
以及其他主题,但未能打印我的 iframe 内容
最佳答案
使用
<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 是一个独立的页面,它不能被 windows 类访问 这就是为什么使用父类
关于javascript - 打印iframe内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36645879/